One of my favorite features in VBA is the so-called "Edit and Continue."
That's the feature that allows you to set a breakpoint in code, then step through the code one line at a time. The next line of code to be executed is highlighted in yellow. While the program is in break mode, you can wander around the rest of the codebase.
I often wander so far that I forget what line I was troubleshooting in the first place.
When I first started debugging VBA, I would just press F8 to execute the next line of code. That would bring the focus back to wherever I left off. Blindly executing a line of code while debugging is not ideal, though.
Luckily, there is a better way to find that yellow highlighted line of code.
Show Next Statement
At the bottom of the Debug menu, there is a command named "Show Next Statement."
When you click on that command, the VBIDE window will shift focus to the yellow-highlighted line of code, which is the next statement that will be executed when you press [F8] to "Step Into."
Shortcut Key - [Alt] + [D], [X]
As an alternative to the menu, you can use the shortcut [Alt] + [D], [X] (Debug > Show Next Statement).
Customizing the Debug Toolbar
I can never remember the shortcut key, so I like to have the command one click away by adding it to the Debug Toolbar. Here are the steps:
- View > Toolbars > Customize ([Alt] + [V], [T], [C])
- _| Toolbars |_ > [√] Debug
- _| Commands |_ > Categories: Debug > Commands: Show Next Statement
- Click and drag "Show Next Statement" from the Customize dialog and drop it on the Debug toolbar
You can drop it anywhere you want on the toolbar, but I like to put it between the Break and Reset icons:
Only Available While Debugging
Be aware that if you are not in break mode, the command does not do anything, and so it will appear grayed out on the toolbar.
Show Next Statement in Action
Here's a short video clip showing the feature in action: