BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
This series of blogs is intended to provide online training in how to debug in Visual Basic for Applications, including using breakpoints and the immediate window.
- Debugging Macros
- Ways to Step Through Code in VBA (this blog)
- Setting Breakpoints and the VBA Stop Statement
- Debug.Print and the Immediate Window
- Other Useful Debugging Tools
Posted by Andy Brown on 25 November 2011
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Ways to Step Through Code in VBA
You can use various combinations of the F8 key to step through code, and also use the yellow arrow and right mouse button to change the next executable statement.
To learn much more about programming and debugging in VBA, book onto one of our classroom or online VBA courses.
Keys to Step through Code
As mentioned, you can use the F8 key to step through code, but what happens when you reach a call to another routine?
In the example shown, the CheckAnagrams macro is about to call the GetWords macro. What will happen next depends on which function key you press:
If you step into the GetWords routine as above by pressing F8 and then realise that you didn't mean to do this, you can press - wait for it - SHIFT + CTRL + F8 to execute the rest of the GetWords routine and return the current line to the main CheckAnagrams program.
Changing the Next Executable Line
The above keys are great for stepping forward through a macro line by line, but whoever said you had to do this? What happens if you want to go backwards, or jump forwards? The easiest way to do this is to click and drag on the yellow arrow:
Want to run the GetWords routine again? Just drag the yellow arrow to the line which you want to execute next, and Excel will make this the next executable statement.
It hardly needs saying, but you should do this with care only!
Another way to achieve the same effect (though not nearly as much fun) is to right-click on the line which you want to execute next:
Right-click on any executable line to make this the next statement to be executed (it will go yellow).
Once you've got the hang of stepping through macros, it's time to set some breakpoints.