VBA – Run a Macro Line by Line / Step by Step

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on April 24, 2024

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.

vba debug menu: step into

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).

run vba macro step by step

By stepping through each line of code, you can mouse-over variables or objects (Ranges, Worksheets, etc.) to view their current value.

vba mouse over value in break mode

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.

vba 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.

clear all breakpoints in vba

Run to Cursor

Another option to run a Macro step by step is to Run to Cursor with CTRL + F8.

vba run to cursor

Run to Cursor will run to the line where the typing cursor is located.

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples