VBA is the language behind Excel macros. Recorded macros are VBA that Excel wrote for you, but writing code by hand lets you add logic, conditions, loops, messages, and cell actions that a simple recording cannot handle well.
Does this affect you?
Use this for Excel for Microsoft 365, Excel 2021, and Excel 2019 on Windows or Mac. The Visual Basic Editor is similar on both platforms, though shortcuts and menu paths differ.
Open the editor and write a first Sub
VBA macros live inside Sub and End Sub lines.
- Enable the Developer tab from Excel options or preferences if it is hidden.
- Open Developer > Visual Basic, or use Alt+F11 on Windows.
- In Project Explorer, right-click the workbook and choose Insert > Module.
- Type a simple procedure such as Sub HelloWorld(), then MsgBox “Hello from Excel”, then End Sub on separate lines.
- Click inside the procedure and press F5 to run it.
Sub names the macro. End Sub marks where it stops. Everything between those lines runs when the macro starts.
Write to cells and use a variable
These are first building blocks for useful spreadsheet automation.
- Use Range(“A1”).Value = “Hello” to write text into A1.
- Put a number in A1.
- Create a variable with Dim myNumber As Integer.
- Read the cell value into the variable.
- Write myNumber + 10 into another cell, such as A2.
More control
Save as a macro-enabled workbook
A normal .xlsx file cannot store VBA code. Save macro workbooks as .xlsm or the code will be removed.
Step through code
Press F8 in the editor to run one line at a time. This is much easier than guessing where a macro failed.
Respect macro security
Excel disables macros from untrusted files by default. Enable Content only for files you trust, because macros can run code.
Learn from recorded macros
Record a simple task, then open Developer > Macros > Edit to inspect the VBA Excel generated. This is a practical way to learn real syntax.
Sources
- Microsoft Learn – Getting started with VBA in Office (2025)
- Microsoft Support – Enable or disable macros in Office files (2025)
- Microsoft Support – Automate tasks with the Macro Recorder (2025)
