09 · Intro to Recording Macros¶
A macro is a recorded sequence of actions that Excel can replay on demand. The Macro Recorder writes real VBA code behind the scenes — this module records simple macros, inspects the generated code, and covers relative vs. absolute references and assigning a macro to a button.
1. Worked dataset¶
Build this on a sheet named Data, A1:C5:
| A | B | C | |
|---|---|---|---|
| 1 | Item | Qty | Price |
| 2 | Pen | 10 | 2 |
| 3 | Book | 4 | 15 |
| 4 | Bag | 2 | 25 |
| 5 |
2. Recording a formatting macro¶
- Developer → Record Macro. Name it
HighlightHeader, shortcutCtrl+Shift+H, store in This Workbook. - While recording: select
A1:C1, apply bold and a light-blue fill (Home → Fill Color). - Developer → Stop Recording.
- Developer → Macros → select
HighlightHeader→ Edit to open the VBA editor and inspect the generated code. It will resemble: - This confirms the recorder captured exactly two user actions (select range, then two format changes) as three lines of VBA.
3. Absolute vs. relative reference recording¶
- Developer has a toggle Use Relative References. With it
off (default), a macro recorded starting from
A1always selectsA1:C1again, regardless of which cell is active when the macro runs. - Turn Use Relative References on, then re-record: click
A2first, then selectA2:C2and apply italic. Stop recording. - Now if the active cell is
A3and you run this macro, it applies italic toA3:C3instead — because it was recorded relative to the starting cell, not to the fixed addressA2. This distinction matters whenever a macro should act on "the row I clicked," not a hardcoded row.
4. A calculation macro¶
- Record a new macro
AddTotalColumn: inD1typeTotal, inD2type=B2*C2, fill down toD4, then selectD2:D4and apply currency format. - Manual check on the generated formulas:
D2 = 10*2 = 20,D3 = 4*15 = 60,D4 = 2*25 = 50. - Inspect the recorded VBA — the
.FillDownand.NumberFormat = "$#,##0.00"lines are recognizable even without writing VBA by hand, which is the main value of recording: it's a fast way to discover the property names and methods VBA uses for a given manual action, useful groundwork before Level 3's hand-written VBA.
5. Assigning a macro to a button¶
- Developer → Insert → Form Controls → Button. Draw it anywhere,
then in the Assign Macro dialog choose
HighlightHeader. - Clicking the button now re-runs the recorded macro — this is the simplest way to give a non-technical user a one-click action without teaching them the Developer tab or keyboard shortcuts.
Cheat sheet¶
| Task | Path |
|---|---|
| Record a macro | Developer → Record Macro |
| Stop recording | Developer → Stop Recording |
| Toggle relative recording | Developer → Use Relative References |
| View/edit recorded code | Developer → Macros → Edit |
| Run a macro | Developer → Macros → Run, or assigned shortcut/button |
| Attach to a button | Insert Form Control Button → Assign Macro |
How It Actually Works¶
The macro recorder doesn't watch your intentions — it watches the literal
sequence of Excel Object Model calls your UI actions trigger, and writes out
VBA statements that reproduce those exact calls. This is why recorded
macros are usually far more brittle and verbose than hand-written VBA:
clicking a cell during recording generates a .Select followed by an
operation on Selection, faithfully mirroring the two-step "select then
act" nature of mouse interaction, whereas a human writing VBA directly
would just act on the range object (Range("A1").Value = ...) without ever
selecting it — selecting a cell has real UI cost (repainting, scrolling)
that a program doesn't need to pay. It's also why recorded macros often
hard-code absolute references and specific sheet names: the recorder logs
the concrete objects your clicks resolved to at record time, with no
awareness of which parts of that were incidental (which cell happened to be
active) versus load-bearing (which range you meant). Macros are stored as
VBA source code inside a hidden project structure within the workbook file
(which is exactly why saving one requires the .xlsm format — plain
.xlsx has no container for that VBA project at all).
Exercise¶
Record a macro FormatTotals that selects D2:D4 (after building the
Total column from Section 4), applies bold, and adds a border. Run it
via Ctrl+Shift+ (your chosen shortcut) and confirm visually that all
three total cells (20, 60, 50) are now bold with a border, then open
the VBA editor and read the generated Sub line by line to identify
which line corresponds to the border vs. the bold.