VBA – Run a Macro Line by Line / Step by Step
Written by
Reviewed by
When debugging VBA code, I find it very useful to step through my macros, allowing me to view variables and other dynamic values. This tutorial will demonstrate how to do so using features from the Debug Menu.
Step Through Macro Line by Line
To step through a macro line by line: In the Visual Basic Editor, place your cursor inside of the macro and hit F8. This will run the first line of your code. Hit F8 to run each additional line, or F5 to resume without stopping. (If you forget the shortcut F8, you can also find this feature in the Debug Menu, as shown above).
By stepping through each line of code, you can mouse-over variables or objects (Ranges, Worksheets, etc.) to view their current value.
By moving your cursor over Range(“a1”).Value, I can see that the value is empty, which may be a problem for my macro.
Run to Breakpoint
If your macro is much longer, or contains loops, you’ll want to set breakpoints instead. To set a breakpoint, click in the margin to the left of the code area. You’ll see a red dot appear indicating the breakpoint.
Now the Macro will run until it reaches the line containing the breakpoint. Press F5 to finish running the Macro until the end or until the next breakpoint. Press F8 to run line by line.
When you finish debugging your Macro(s), you’ll want to remove all breakpoints so the code runs uninterrupted.
Run to Cursor
Another option to run a Macro step by step is to Run to Cursor with CTRL + F8.
Run to Cursor will run to the line where the typing cursor is located.