Expert Excel · Interactive lesson 5 of 12
VBA automation
A12's recorder writes VBA for you, click by click. Writing it directly is what turns a recording into something maintainable — a loop instead of ten repeated lines, an error handler instead of a macro that just stops.
Screens shown: Excel for Microsoft 365 for Windows. Practice works in any browser.
Written tutorialAutomating Excel with VBASkill check
Try these first:
- Write a Sub with a clear name and a single, obvious purpose.
- Loop over a range instead of writing one line per cell.
- Add an error handler that reports a failure instead of crashing silently.
- Explain why
SelectandActivateare usually a sign a macro can be written more safely.
All familiar? Skip ahead to E6 — Office Scripts & Power Automate.
Concept teach
What you're learning
- Procedure structure
- A
Subdoes something; aFunctionreturns a value. Give each one job and a name that says what that job is. - Variables & constants
Dim total As Doubledeclares a variable with a type;Const MaxRows As Long = 10000names a value that never changes — both beat an unexplained number buried mid-procedure.- Loops
For Each cell In rngwalks every cell in a range;Do Whilerepeats until a condition changes — either replaces ten copy-pasted lines with one pattern that scales.- Working with ranges safely
- Reference
ws.Range("A2:A10")directly instead ofSelectthenActiveCell— safer, faster, and it keeps working even if the user clicked somewhere else first. - Error handling
On Error GoTo ErrHandlercatches a failure and reports it clearly, instead of the macro just stopping mid-task with no explanation.- Events
- Code that runs automatically on a trigger —
Workbook_Open,Worksheet_Change— instead of needing someone to remember to click a button. - User prompts
MsgBoxreports something to the user;InputBoxasks them for something — both keep a macro from failing silently or guessing.- Logging actions
- Writing a timestamped line to a Log sheet every time a macro runs — so six months from now, someone can see exactly when it last worked and what it did.
Where to find it in Excel
Developer›Visual Basic (Alt+F11)
| A | B | C | D | E | F | G | H | ||
|---|---|---|---|---|---|---|---|---|---|
| 1 | Region | Sales | |||||||
| 2 | North | 6,900 | |||||||
| 3 | South | 5,400 | |||||||
| 4 | |||||||||
| 5 | |||||||||
| 6 | |||||||||
| 7 | |||||||||
| 8 | |||||||||
| 9 | |||||||||
| 10 |
Swipe sideways to see the whole window.
- 1Developer tab (File › Options › Customize Ribbon to turn it on).
- 2Visual Basic (Alt+F11) opens the editor; Insert › Module adds a code module.
- 3Option Explicit, a loop over a Range without Select, error handling and a log call.
- 4Macros (Alt+F8) runs a procedure; event code goes in the sheet or ThisWorkbook module.
Rendered view of Excel for Microsoft 365 (Windows). Ribbon layout can differ slightly on Mac, Excel for the web or a narrow window.
Guided build
Fill in the four blanks to complete a macro that refreshes data, timestamps it, exports a PDF, and reports failure clearly.
Independent extension
Write a macro that could actually ship
Write a macro that refreshes a real workbook's data, stamps a timestamp, and exports a PDF — with an error handler that reports exactly what failed instead of leaving Excel stuck mid-task. Add one logged line to a Log sheet every time it runs.
Spot a mistake or have an idea for this lesson? Send feedback