Code in Excel is called VBA (Visual Basic for Applications); “macro” is a synonym. A macro is written code that executes commands in Excel.
Why code
- To automate repetitive tasks (e.g. formatting a monthly report with one click).
- To do things the ribbon can’t (e.g. a custom function that spells out a number).
Setting up
- Save the workbook as .xlsm — the .xlsx format does not keep macros.
- Show the Developer tab: right-click the ribbon → Customize the Ribbon → check Developer.
- Security: “Disable all macros with notification”, then “Enable Content” when opening the workbook.
Recording a macro
The Record Macro button (Developer → Code or on the status bar) works like a tape recorder: whatever you do is turned into VBA. Don’t forget to stop recording.
The Use Relative References button: when on, movement relative to the active cell is recorded (Offset); when off, absolute addresses (Range("A1")).
Keys
| Alt+F11 | VBA editor |
| Alt+F8 | list of macros to run |
| F8 in the editor | step through the code |
Sample macro
Sub FormatReport()
Range("A1:D1").Font.Bold = True
Range("A1:D1").Interior.Color = RGB(33, 115, 70)
Columns("A:D").AutoFit
ActiveCell.Offset(1, 0).Select ' relative movement
End Sub