How to Build Excel Macros: Step-by-Step Guide
In this article
You build an Excel macro by recording a sequence of actions with the macro recorder, then editing that recorded code in the VBA editor to make it flexible. The recorder is the fastest way in: turn on the Developer tab, click Record Macro, perform the task once, stop recording, then press the assigned shortcut or open View Macros to run it again on other data. Everything below walks through that workflow plus saving, editing and debugging the result — the parts where most first attempts break.
Excel's macro language is VBA (Visual Basic for Applications). It ships with every Windows desktop copy of Excel; on Excel for Mac the editor is available too, but a few features differ, noted where relevant. Labels and ribbon positions can vary slightly between Excel versions, so if a button is not exactly where described, look for the same name elsewhere on the ribbon. Excel on the web and the mobile apps cannot create or run VBA macros.
What You'll Need
- Desktop Excel on Windows or Mac. The browser and phone versions do not support VBA.
- A workbook you can safely experiment in — copy your real file first.
- The Developer tab enabled (Step 1 below).
- A clear idea of the task, done once manually so you know the exact clicks involved.
- If your organisation blocks macros, a way to ask IT — many companies disable them by policy, and no local setting overrides that.
Step-by-Step
- Back up your file. Save a copy of any workbook you are about to automate. Recorded macros cannot be undone with Ctrl+Z, and a badly scoped loop can overwrite a lot of data quickly.
- Enable the Developer tab. Right-click anywhere on the ribbon and choose Customize the Ribbon, then tick Developer in the list on the right and click OK. On Mac, open Excel's preferences and enable the Developer tab in the Ribbon section. You only do this once per install.
- Decide whether the recorded macro will just repeat a fixed task or needs to work on changing data. This matters because the recorder hard-codes cell addresses. If your data moves, you will need to edit the code afterwards.
- Click Developer > Record Macro. Give it a clear name with no spaces (for example CleanSalesData), and optionally assign a keyboard shortcut in the Shortcut key box. Use Ctrl plus a letter; avoid shortcuts Excel already uses, such as Ctrl+C or Ctrl+S.
- Perform the task slowly and exactly once. Type values, apply formatting, sort, filter — whatever the job is. Every click and keystroke on the sheet becomes a line of code. Avoid switching windows or scrolling idly while recording, because those actions get recorded too.
- Click Developer > Stop Recording when finished.
- Test it. Open Developer > Macros (or press Alt+F8 on Windows), select your macro and click Run. Press your shortcut if you assigned one. Watch for the result you expected.
- Open the code. In Developer, click Visual Basic (or press Alt+F11 on Windows) to open the VBA editor. In the Project pane, expand your workbook, then Modules, and double-click the module holding your macro. The recorded code appears as a Sub with Begin and End lines.
- Read it and add comments. Lines beginning with an apostrophe are comments and do nothing. Add a short comment above each block explaining what it does; it saves enormous confusion later.
- Replace hard-coded ranges with something flexible. Recorded code often looks like Range("A1:D200").Select. A safer pattern is to work with the UsedRange or to find the last row dynamically, for example with Cells(Rows.Count, 1).End(xlUp).Row. Turn off screen updates during long loops with Application.ScreenUpdating = False at the start and back to True at the end.
- Save in the macro-enabled format. Use File > Save As and pick Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file silently discards all VBA code when saved.
- Run it on sample data and check the output cell by cell. Compare the macro's result against the same task done by hand at least once before trusting it on real data.
- Handle errors so it fails loudly, not quietly. Add On Error GoTo and a message box at the end so you know if a run stopped partway. Silent failures are the main reason people lose trust in their own macros.
- Keep a plain-language note in the workbook. A cell on a hidden sheet or a text box listing each macro, its shortcut and what it does makes the file maintainable by someone else.
Common Mistakes
- Saving as .xlsx. Excel warns you once, but if you click past the warning the code is gone. Always choose .xlsm.
- Recording with a range selected when you start. If cells are already highlighted, the recorded Selection may apply to the wrong area on the next run. Click a single cell before recording.
- Trusting absolute references. Recorded macros repeat exact addresses. If the data grows by rows, the macro processes only the original block.
- Skipping Application.ScreenUpdating = False in loops. Macros that flicker between every row can run many times slower than necessary.
- Leaving mouse clicks in the code. Scrolling or clicking around during recording adds pointless Select statements that slow execution and confuse edits.
- Using generic names like Macro1. A dozen Macro1, Macro2 entries make it impossible to know which does what.
- Assuming macros travel with copy-paste. Pasting data into a new workbook does not bring the macros; you must copy the module in the VBA editor or save the workbook itself.
Tips
- Record first, refine second. Get a working draft from the recorder, then simplify the code — it is far easier than writing from a blank module.
- Break a big task into small macros and call them from one master macro. Smaller pieces are easier to test and to fix.
- Use meaningful variable names (lastRow, dataSheet) rather than a, b, c.
- Press F8 in the VBA editor to step through one line at a time — the best way to see exactly where a macro does something unexpected.
- Test on copies, never on the only version of an important file.
- If your macro needs to run when the workbook opens or is edited, place it in the ThisWorkbook or sheet module as an event procedure rather than a standard module.
- Share the logic, not the file, if the recipient's security settings block macros; explain the steps so they can decide whether to trust it.
Troubleshooting
The macro does nothing when run. Check you are on the sheet and range the macro expects, confirm the code saved (look for the module in the VBA editor), and press F8 to watch each line execute.
Excel shows a security warning and macros are disabled. Excel blocks macros in files from the internet. If you trust the source, right-click the file in File Explorer, open Properties, tick Unblock, and apply. In Excel's Trust Center settings, you can add a trusted location folder — the safest approach is to keep your own macro workbooks in a folder you trust rather than lowering global security for everything.
It worked once and now breaks. Almost always the data changed shape — extra rows, a new column, or blank cells where the recorder assumed values. Re-record or edit the range logic.
The file opens but the macro is missing. You likely saved as .xlsx. Save a copy as .xlsm and re-add the code.
Shortcut does nothing or triggers something else. Your chosen key combination is already taken by Excel or an add-in. Reassign a different letter, or run it via Developer > Macros.
Macros are greyed out entirely. Your organisation's policy has disabled VBA. Ask IT; there is no legitimate way around a deliberate admin restriction, and attempting one risks breaking your install.
Frequently Asked Questions
Can I create macros in Excel on the web or mobile?
No. VBA macros require the desktop app on Windows or Mac. The web and mobile versions can open and display workbooks that contain macros but cannot create, edit or run them.
Do I need to learn to code?
No to start. The recorder produces working code from your clicks. Basic reading of VBA — recognising a Range, a loop and an If statement — is enough to make most recorded macros far more reliable.
Why does Excel warn about macros?
Because macros can run code, including malicious code, they are treated as a security risk. Only enable them in files from sources you trust, and keep macro workbooks in a trusted folder.
Can a macro be undone with Ctrl+Z?
No. Macro actions are not added to the undo history. Save a backup before running anything that overwrites data.
Will macros work if someone opens the file in Google Sheets?
No. Google Sheets does not run VBA macros, though it has its own Apps Script. If sharing matters, confirm the recipient uses desktop Excel.
How do I see what a recorded macro actually did?
Open the VBA editor with Alt+F11 on Windows, find the module in the Project pane, and read or step through the Sub line by line with F8.