Excel VBA debugging: find bugs with stepping and the Locals window
The hardest bugs are the silent ones: the macro runs to the end, but the result is wrong. The only way in is to look, line by line, with the values of your variables in view. That is what the VBA editor gives you with stepping, breakpoints, the Immediate window and the Locals window.
In short: F8 runs the macro one line at a time, and the yellow line is the one that runs next. F9 sets a breakpoint where the macro pauses. Debug.Print value writes to the Immediate window (Ctrl+G). The Locals window lists every variable with its value and type.
What you need to know: variables, procedures with arguments and error handling.
Try it right now
The simplest tool first: Debug.Print. Before a macro changes anything, have it print what it is about to work with. The result of this exercise is only what ends up in the Immediate window.
Task Before you let a macro loose on real data, you want to see what it is working with. Write the macro Inspect so that it changes nothing on the sheet but writes two lines to the Immediate window: first "Last row: 6", then "Total: 4625" (the last used row of column A and the total of column B, both worked out from the data).
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Invoice | Amount | ||
| 2 | INV-1043 | 1250 | ||
| 3 | INV-1044 | 380 | ||
| 4 | INV-1045 | 2140 | ||
| 5 | INV-1046 | 95 | ||
| 6 | INV-1047 | 760 |
The tools at a glance
| Tool | Key / menu | Use it to |
|---|---|---|
| Step Into | F8, Debug > Step Into | Run one line, then pause. Walks into procedures being called. |
| Step Over | Shift+F8 | Like Step Into, but runs called procedures in one go. |
| Step Out | Ctrl+Shift+F8 | Run the rest of the current procedure and pause back in the caller. |
| Breakpoint | F9 or click the grey margin | The macro runs normally up to this line and pauses there. Red dot, red line. |
| Continue | F5 | Carry on normally after a pause, to the next breakpoint or the end. |
| Immediate window | Ctrl+G, View > Immediate Window | Shows Debug.Print output. You can type commands here too. |
| Locals window | View > Locals Window | All variables of the current procedure with value and type; arrays expand. |
| Watch | Debug > Add Watch | Keep an eye on one expression in the Watches window, even across procedures. |
While the macro is paused, hovering over a variable shows its value. In the Immediate window, ?total prints the current value and ?Cells(5, 2).Value the contents of a cell.
Step through in the exercises
The exercises here have a Step through button (or F8 in the code). The macro runs once, and then you move through every line with Next and Back. The yellow line runs next; on the left you see the sheet at that moment, on the right the Locals window with every variable. The real editor cannot undo a step, by the way: whatever a line wrote to the sheet stays there.
Find the bug: loop one short
The macro runs without any message, only the last row stays empty. Step through it and watch the value of lastRow and when the loop ends.
Task Find the bug: the macro Commission should put 5% of sales in column C for every salesperson. It runs without any message, but the last row stays empty. Use "Step through" and watch i and lastRow in the Locals window. Then fix the faulty line.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Salesperson | Sales | Commission | |
| 2 | Jake Miller | 18,400.00 | ||
| 3 | Emma Clarke | 22,750.00 | ||
| 4 | Ravi Patel | 9,600.00 | ||
| 5 | Sophie Turner | 31,200.00 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Find the bug: wrong variable
Here a total does end up in the cell, just the wrong one. The Locals window gives it away: a variable shows up that should not exist at all.
Task Find the bug: the macro TotalCosts should add up the costs of all months and write the total to E1. But afterwards E1 only holds the last month's value. Use "Step through" and look in the Locals window to see which variables exist and what they hold.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Month | Costs | Total | |
| 2 | January | 1,840.00 | ||
| 3 | February | 1,615.00 | ||
| 4 | March | 2,030.00 | ||
| 5 | April | 1,790.00 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Option Explicit: catch typos before the macro starts
With Option Explicit at the top of the module, every variable has to be declared with Dim. A typo like totl then shows up at compile time: VBA reports "Compile error: Variable not defined" and highlights the name. In the VBA editor, tick Tools > Options > Editor > Require Variable Declaration and the line is added to every new module. Existing modules do not get it that way; add it there yourself.
In real Excel
This is how you tackle a macro that calculates wrong:
- Set a breakpoint: click the grey margin next to the first suspicious line, or put the cursor in it and press F9.
- Start: F5 runs the macro up to the breakpoint.
- Look: open the Locals window (View > Locals Window). Are the values right?
- Step on: press F8 line by line until a value differs from what you expect. That is where the bug is, or just before.
- Stop: Run > Reset (the blue square in the toolbar) ends the run. Breakpoints stay until you close the file; Ctrl+Shift+F9 clears them all.
In long loops, F8 gets tedious. A conditional pause helps: in the Watches window you can tell VBA to break as soon as an expression such as i = 250 becomes true or a value changes. Or temporarily put If i = 250 Then Stop inside the loop.
Common mistakes
One too few or one too many
Loops that end at lastRow - 1 or start at row 1 instead of 2 are the most common logic bug. The exercise check then reports something like "C5 is empty, expected: 1560" together with a note that the last row is missing.
Typo in a variable name
Without Option Explicit, a new empty variable appears silently. In the Locals window you spot it because it is listed although no Dim line mentions it.
Leaving Debug.Print in
Debug.Print does no harm, and nobody looks at the Immediate window in daily use. Lots of output inside a big loop does slow the macro down, though. Delete check output once the bug is found, or put an apostrophe in front to turn it into a comment.
Stop in a finished macro
A forgotten Stop pauses the macro for colleagues too and opens the editor. Search for it with Ctrl+F before you hand the file on.
FAQ
Debug.Print or MsgBox?
MsgBox stops the macro until someone clicks OK and is meant for messages to users. Debug.Print writes quietly to the Immediate window and carries on. For looking inside, Debug.Print is the better choice, especially in loops.
Can I change code while the macro is paused?
Often yes: the editor accepts many edits, and F5 or F8 continues with the new code. Some changes, such as to Dim lines, need a restart, and the editor asks first.
What is the yellow arrow?
It marks the line that runs next. You can drag it to another line (Set Next Statement) to repeat or skip a line. Careful: the variables keep their current values.