To create a macro in Microsoft Excel for Microsoft 365 on Windows, choose View > Macros > Record Macro, give it a name, perform the actions you want to automate, then choose Stop Recording. For more control, use Excel’s Visual Basic for Applications (VBA) editor to write or edit the macro’s instructions.
What an Excel macro does
A macro is a saved set of actions or instructions that Excel can run again. It can handle repetitive work such as applying consistent formatting, preparing a report, cleaning a worksheet, or copying information into a standard layout. Recorded macros are translated into VBA code, so recording is a way to generate a starting point even if you do not know how to program.
Macros are useful when the same sequence of steps must be repeated, but they are not the right tool for every spreadsheet task. A formula recalculates when its inputs change; a macro runs only when you start it or configure a suitable trigger. For a simple calculation or an automatic visual rule, a formula or conditional formatting may be easier to maintain.
Record a macro step by step
- Open the workbook and, if practical, make a copy before recording. Set up the sheet so the macro can be tested on representative data.
- On the ribbon, select View > Macros > Record Macro. You can also use Developer > Record Macro if the Developer tab is displayed.
- Enter a name beginning with a letter. Macro names can include letters, numbers, and underscores, but cannot contain spaces. Optionally assign a shortcut key, choose where to store the macro, and add a description.
- Carry out the actions to be recorded, in order. Excel captures eligible actions such as selecting cells, entering values, and applying formatting.
- Select View > Macros > Stop Recording (or the stop control on the status bar, if shown).
- Run the macro on a copy or test worksheet and inspect the result before using it on important data.
The recorder does not understand the purpose of your work; it records actions. It may include unnecessary selections or depend on a particular starting cell. If the steps should work across different worksheets or changing data sizes, inspect and improve the generated VBA rather than assuming the recording is flexible.
Choose where the macro is stored
The storage choice determines which workbooks can use the macro:
- This Workbook: stores the macro in the current file. The macro travels with that file, but not with other workbooks.
- Personal Macro Workbook: stores it in a hidden workbook that opens when Excel starts on that computer. This is convenient for personal macros used across files, but it is not automatically available to colleagues or on another computer.
- New Workbook: creates a separate workbook to hold the recorded macro.
A workbook containing VBA generally needs a macro-enabled format. Use File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to preserve macros in the file. Saving a macro-containing workbook as a standard .xlsx file removes its VBA project; heed Excel’s format warning rather than assuming the macro will remain.
Edit or write a VBA macro
To see recorded code, open Developer > Visual Basic, or press Alt+F11 on Windows. In the Visual Basic Editor, find the workbook’s project and its Modules folder. A recorded macro is usually stored in a standard module. You can review the code, make changes, and return to Excel to run it from View > Macros > View Macros.
For example, this short macro puts a heading in cell A1 of the active worksheet and makes it bold:
Sub AddReportHeading()
' Write a heading to the active worksheet.
Dim targetSheet As Worksheet
Set targetSheet = ActiveSheet
targetSheet.Range("A1").Value = "Monthly Report"
targetSheet.Range("A1").Font.Bold = True
End Sub
VBA instructions can target a specific worksheet, loop through rows, test conditions, and display messages, making them more adaptable than a basic recording. The example deliberately uses ActiveSheet; that means it affects whichever sheet is active when you run it. For important work, explicitly refer to the intended worksheet and validate the workbook’s structure so the macro cannot silently change the wrong data.
Run macros and use shortcuts
In Windows Excel, open View > Macros > View Macros, select a macro, and choose Run. A macro can also be assigned to a button or a keyboard shortcut. During recording, Excel allows a shortcut assignment; choose carefully because a shortcut can override an existing Excel shortcut while the workbook is open. Avoid assigning a shortcut that you rely on for a common command.
On Mac, the ribbon and Visual Basic Editor are available, but keyboard shortcuts differ and may conflict with macOS shortcuts. Use the Tools > Macro menu or the Macros command on the ribbon if a Windows shortcut does not apply. Excel for the web can open and edit workbooks containing VBA, but it cannot create, run, or edit VBA macros. Open the file in the desktop Excel application to work with VBA.
Security, testing, and common problems
Macros can change or delete data and can run harmful code, so do not enable macros in an unexpected file simply because Excel displays a prompt. Only run VBA from a source you trust, and follow your organization’s security policy. Excel may block macros in files from the internet or apply security settings managed by an administrator; do not bypass those protections without confirming the file is safe.
Test a new macro on a duplicate workbook with sample data. Check the starting selection, the sheet and workbook it acts on, and the result when a cell is blank or the data has more or fewer rows. A recorded macro that refers to fixed cell addresses may fail or affect the wrong range when the worksheet changes. Add clear comments and keep a copy of the original file.
Macros cannot be undone with Ctrl+Z after they run. If a macro makes an unwanted change, close the workbook without saving if appropriate, or restore a backup. This is why a test copy and a saved original matter, especially for macros that delete, overwrite, or move information.
Frequently Asked Questions
Do I need to know VBA to create a macro?
No. You can record a straightforward series of actions without writing code. VBA knowledge becomes useful when the macro needs decisions, loops, flexible ranges, or clearer handling of changing data.
Why did my macro disappear after saving?
It may have been saved in an .xlsx workbook, a format that does not retain VBA macros. Save the file as an .xlsm workbook, or confirm that the macro was intentionally stored in the Personal Macro Workbook.
Can I run an Excel macro in a browser?
No. Excel for the web does not run VBA macros. Use the desktop Excel application to create, edit, or run VBA, though the web version can work with the workbook’s ordinary spreadsheet content.
Can I undo a macro with Ctrl+Z?
No. Excel clears its undo history when a VBA macro runs, so Ctrl+Z will not reverse the macro’s changes. Save a backup and test the code on a copy first.
What is the difference between a macro and a formula?
A formula calculates a result from cell values and updates as inputs change. A macro performs a sequence of actions when run; it can change cells, formats, or workbook structure but does not automatically recalculate just because an input changed.