A macro records your actions so you can replay them with one click — automating repetitive tasks. Under the hood it's VBA (Visual Basic for Applications).
Automation concept: repetitive tasks handled by code
View tab → Macros → Record Macro
Name: FormatReport
Shortcut: Ctrl+Shift+R
→ do your steps (format, sort, etc.)
→ Macros → Stop Recording
Now CtrlShiftR replays every step instantly.
Open Developer → Visual Basic (or AltF11) to see the generated code:
1Sub FormatReport()
2 Range("A1:D1").Font.Bold = True
3 Range("A1:D1").Interior.Color = RGB(33, 115, 70)
4 Columns("A:D").AutoFit
5End SubEach line is one recorded action. You can edit it directly.
1Sub SayHello()
2 MsgBox "Total rows: " & Cells(Rows.Count, 1).End(xlUp).Row
3End SubMsgBox pops up a message; this one reports how many rows of data exist.
Workbooks with macros must be saved as .xlsm (macro-enabled). A normal .xlsx silently discards macros on save. Also, macros from unknown sources can be malicious — only enable macros from files you trust.
Record first, then read the code. The macro recorder is the best way to learn VBA — do the task by hand, then study what Excel wrote.