Macros in Excel: Definition and How to Create One
In this article
A macro in Excel is a saved set of instructions that runs automatically to perform a task for you: recording your clicks and keystrokes as a small program, then replaying them on demand. Microsoft defines macros this way in its own documentation, and the practical definition is simply "a shortcut that repeats work exactly the same way every time." Excel records macros in the VBA (Visual Basic for Applications) programming language, stores them inside the workbook or in your Personal Macro Workbook, and runs them from a button, a keyboard shortcut or the Developer tab. Macros are ideal for repetitive formatting, cleaning imported data, building recurring reports or filling in the same formulas across many sheets.
What You'll Need
- Excel on Windows desktop. This is the important one: the macro recorder is available in Excel for Windows. Excel for Mac has a different, more limited recorder and no full VBA editor in some builds, and Excel for the web cannot record or run macros at all.
- The Developer tab enabled, or you can use the Record Macro button under View > Macros.
- A workbook saved in the .xlsm (macro-enabled) format if you want macros to persist. Standard .xlsx files strip macros out.
- A clear idea of the exact steps you want to automate, written down, so you record them in the right order.
Step-by-Step
- Turn on the Developer tab. Right-click anywhere on the ribbon and choose Customize the Ribbon. In the list on the right, tick Developer and click OK. On Windows this is the fastest route; the exact wording of the option can differ slightly between versions, but the path is stable. Alternatively, skip this and use the Record Macro button under the View tab, in the Macros dropdown.
- Plan the macro before you record it. Write the sequence on paper: which sheet, which range, which formatting. Think about whether the steps depend on a specific cell being selected. A macro records what you do, not what you intend, so if you click A1 while recording, it will always jump to A1 when it runs.
- Start recording. On the Developer tab click Record Macro. In the dialog, give the macro a name with no spaces (for example FormatReport). Optionally assign a shortcut key. Note that shortcut letters like Ctrl+letter usually override Excel's built-in shortcuts, so pick something you do not use daily. Choose where to store it: This Workbook keeps it with the file, New Workbook creates a fresh file for it, and Personal Macro Workbook makes it available in every workbook you open on that PC.
- Perform the steps once, slowly and cleanly. Every click and keystroke is captured. Avoid correcting mistakes while recording, because the corrections get recorded too. Avoid switching windows or scrolling with the mouse if you can help it; use keyboard navigation or the Name Box to move around.
- Stop recording. Click Stop Recording on the Developer tab, or click the small stop square at the bottom-left status bar. Do not leave the recorder running while you do something else.
- Test it on a copy of your data. Press Ctrl+Z first to undo the recording session if it changed your sheet, or undo manually. Save a copy of the workbook, then run the macro with Developer > Macros, select it and click Run. If you assigned a shortcut, use that. Check that the result matches what you expected and that no data was overwritten.
- Save in .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. If you save as .xlsx, Excel warns that macros will be removed. Save it, and the macro survives.
- Optionally edit the code. Press Alt+F11 to open the Visual Basic editor, find your macro under Modules, and read it. The recorded code is often verbose and clumsy, but small edits (renaming a range, removing a stray Select) make it faster and easier to maintain. Close the editor and save again.
- Run it from a button (optional). On the Developer tab click Insert, choose a Form Control button, draw it on the sheet, and assign your macro in the dialog that appears. This makes the macro usable by anyone who does not want to open the macro list.
Common Mistakes
The most frequent error is recording before thinking, so the macro hard-codes cell references that only worked in that one dataset. Recording is a literal capture: "select A1, type 100" becomes a permanent instruction, not a relative one. If you need flexible behaviour, use the Relative References toggle on the Developer tab before recording so the macro works from the current cell instead of an absolute address. Another common mistake is saving as .xlsx and losing the macro, or storing it in Personal Macro Workbook and then wondering why it is missing on another computer. Also, avoid leaving the recorder running, because every subsequent action, including switching to another file, becomes part of the macro. Finally, do not run an untested macro on your only copy of important data: test on a duplicate first.
Tips
- Give macros descriptive names and, if you make many, add a comment line in the VBA editor explaining what each one does.
- Record small macros and combine them rather than one giant recording; they are easier to debug.
- Use Ctrl+Break to interrupt a macro that is running or stuck in a loop.
- If you share workbooks, tell recipients the file contains macros so they can decide whether to enable them, and never enable macros from an unfamiliar source.
- Keep a plain copy of the original data in a separate workbook before running anything that deletes rows or columns.
Troubleshooting
If the macro does not appear in the list, check you saved in .xlsm and that the macro was stored in This Workbook rather than a file that has since been closed. If Excel blocks macros with a security warning bar, that is the Trust Center protecting you; review the file's origin and only enable content if you trust it. If the macro runs but does nothing, check the sheet it expects is the active sheet, since recorded macros act on whatever sheet is open. If it stops with an error dialog, click Debug to jump to the failing line. If a shortcut does nothing, another macro or Excel's own shortcut may be using that key combination, so reassign it. If macros are disabled entirely by your organisation's policy, the Developer tab options may be greyed out, and that is a settings decision for your administrator rather than something to work around.
Frequently Asked Questions
Is recording a macro the same as programming?
No. Recording generates VBA code automatically from your actions, so you get the benefit without writing code. Editing that code is optional.
Do macros work in Excel for the web?
No. Excel for the web cannot record or run VBA macros. Use the Windows desktop app for macro work, and note that Mac support is more limited.
Can a macro be undone?
Only partially. Pressing Ctrl+Z after running a macro may undo nothing at all, because the whole macro often counts as a single action. Always work on a copy of important data.
Why does Excel say macros are disabled?
Excel's Trust Center blocks macros by default for files from other sources, and some organisations disable them entirely. Enable content only for files you trust.
Where are my macros stored?
In the workbook itself, or in your Personal Macro Workbook on that computer if you chose that option. Personal macros do not travel with a file you email.