Microsoft Office Tutorials and References
In Depth Information
Weapons of Mass Debugging
To set a breakpoint in your code, click your mouse into the line of code where you want the
breakpoint to start. Click the Toggle Breakpoint button, or press the F9 key, and the breakpoint will be
set at that line. VBA clearly identifies a breakpoint with a large brown dot in the Code window’s left
margin, and the code line itself is shaded brown.
For example, if you suspect a bug in a macro but you know that the majority of the macro runs
without any problems, you can set a breakpoint starting at a section in the program where you
want to examine the code more closely. In Figure 17-12, I clicked my mouse into the code line With
ActiveSheet.Sort and clicked the Toggle Breakpoint button. If the macro were to be run now, it
would execute all lines of code up to, but not including, that breakpoint line. Now, you can step
through the subsequent lines of code to verify that each line is doing what you’d expect.
You can set a breakpoint only on an executable line. Commented lines in your
code, or empty lines, cannot be set as breakpoints.
True to its name, the Toggle Breakpoint button can be clicked again to clear the current breakpoint
with any portion of that line selected, or you can click the large dot in the Code window’s margin.
You’ll notice that if you have already set a breakpoint and you click the Toggle Breakpoint button,
or press F9, you will set another breakpoint if you have any other line of code selected. You can set
more than one breakpoint, so to quickly clear all breakpoints at once, press the Ctrl+Shift+F9 keys.