How to Write a Macro in Excel (Step-by-Step)

VBA & macros · Updated November 23, 2026

Writing a macro in Excel lets you record or code a sequence of actions and replay them with a single click or keyboard shortcut. This guide covers the fastest path for beginners—using the Macro Recorder—and then shows how to read and edit the generated VBA code so you can tweak it later. You’ll need Excel 2016 or later (the steps are nearly identical in Microsoft 365).

What You’ll Need

  • Excel for Windows (Microsoft 365, 2021, 2019, or 2016). Mac steps differ slightly and are noted where relevant.
  • A workbook you’re willing to experiment with—save a copy first because macro actions can’t be undone with Ctrl+Z once you run them.
  • The Developer tab visible. If you don’t see it, right-click any ribbon tab, choose Customize the Ribbon, check Developer in the right pane, and click OK.

Step-by-Step

  1. Plan the exact steps you want to record. Write them down. The recorder captures every mouse click, keystroke, and selection, so a stray click can mess up the macro. Decide the starting cell and the exact range you’ll work with.
  2. Open the Macro Recorder. Go to the Developer tab and click Record Macro (or click the Record Macro button on the status bar at the bottom-left). In the dialog that appears, give the macro a name—no spaces, start with a letter, e.g., FormatSales.
  3. Set a shortcut and storage location. In the same dialog, you can assign a shortcut key like Ctrl+Shift+F (avoid overriding built-in shortcuts like Ctrl+C). For Store macro in, choose This Workbook to keep it in the current file, or Personal Macro Workbook to make it available in all your workbooks. Click OK to start recording.
  4. Perform the actions you want to automate. For example, select a range, apply bold, set a fill color, adjust column widths, or enter formulas. Use only the keyboard and mouse—avoid using the scroll wheel or clicking on other sheets unless you want those actions recorded.
  5. Stop recording. Go back to the Developer tab and click Stop Recording (or click the stop button on the status bar). Your macro is now saved.
  6. Test the macro. Select a different cell or sheet, then run the macro by pressing the shortcut you set, or by going to Developer > Macros, selecting the macro, and clicking Run. Watch for errors or unexpected results.
  7. Edit the macro in VBA (optional). Press Alt+F11 to open the Visual Basic for Applications (VBA) editor. In the Project Explorer (left pane), expand the module where your macro lives (usually Module1 under your workbook). Double-click it to see the code. You can modify the code directly—for example, change the range from Range("A1:D10") to Range("A1:D20").
  8. Save your workbook as a macro-enabled file. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown. If you save as .xlsx, Excel will strip out the macros.

Common Mistakes

  • Not planning ahead. Recording without a clear plan captures extra actions that make the macro fragile.
  • Using relative references incorrectly. By default, the recorder uses absolute references—the macro always acts on the exact cells you recorded. If you need the macro to work on whatever cell is selected, turn on Relative Reference on the Developer tab before recording (on Mac: Developer > Use Relative References).
  • Forgetting to save as .xlsm. If you save as .xlsx, the macro is silently discarded.
  • Overwriting a built-in shortcut. Assigning Ctrl+S or Ctrl+Z will cause confusion because your macro will run instead of the normal command.
  • Editing VBA without understanding it. A small typo can break the macro. Always test after editing.

Tips

  • Name your macros descriptively, like ApplyInvoiceFormat, not Macro1.
  • Use relative references when you want the macro to apply to the active cell. For example, if you record selecting A1 and then formatting it, with relative references the macro will format whatever cell you’ve selected when you run it.
  • Keep macros short. If you need a long sequence, break it into smaller macros and call them from a master macro.
  • Add comments in VBA by typing an apostrophe (') at the start of a line. This helps you remember what each section does.
  • Test on a copy of your data first. Macros can’t be undone with Ctrl+Z after they run.
  • For tasks that repeat across many workbooks, store the macro in your Personal Macro Workbook so it’s always available.

Troubleshooting

  • Macro doesn’t appear in the list. Make sure you saved the workbook as .xlsm and that the macro is stored in the correct location (This Workbook vs. Personal).
  • “Macros are disabled” error. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and choose Enable all macros (or better, enable with notification and then allow the macro when prompted).
  • Macro runs but does nothing. Check if you used absolute references and the target cells are empty or hidden. Also verify the macro is actually assigned to the button or shortcut you’re using.
  • Runtime error when running. Open the VBA editor (Alt+F11), click anywhere in the macro code, and press F8 to step through line by line. The yellow highlight shows the line that’s failing. Common fixes: correct a range name, fix a typo, or close any open dialog boxes.
  • Macro works on my PC but not on a colleague’s. This usually happens because the workbook is missing the macro (they got the .xlsx version) or because their Excel security settings block macros. Send them the .xlsm file and ask them to enable macros.

Frequently Asked Questions

  • Can I write a macro without recording? Yes, you can type VBA code directly in the VBA editor. But recording is easier for beginners because it generates the code automatically.
  • What’s the difference between a macro and a VBA procedure? A macro is essentially a VBA procedure (usually a Sub) that automates tasks. Recording creates the Sub; you can also write your own.
  • Can macros be used in Google Sheets? Google Sheets uses Apps Script (JavaScript), not VBA. The recording feature is called Macros, but the code is different.
  • Will my macro work on Excel for Mac? Most recorded macros work, but some Windows-specific commands may not. Also, the shortcut keys differ (use Cmd instead of Ctrl). Test on a Mac before relying on it.
  • How do I delete a macro? Go to Developer > Macros, select the macro, and click Delete. Or open the VBA editor and remove the module.
  • Can I assign a macro to a button on the sheet? Yes. Insert a shape or button from Insert > Shapes (or a Form Control button), then right-click it and choose Assign Macro.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.