Page 75 - Excel for Scientists and Engineers: Numerical Methods
P. 75
52 EXCEL: NUMERICAL METHODS
Tracing Execution
When your program produces an error during execution, or executes but
doesn't produce the correct answer, it is often helpful to execute the code one
statement at a time and examine the values of selected variables during
execution. If your procedure contains logical constructions (If or Select Case,
for example), simply stepping through code will allow you to verify the logic.
Stepping Through Code
There are two ways to begin the process of stepping through the code of a
Sub procedure:
Select the name of the procedure in the Macro Name list box and press the
Step Into button. This will display the code module containing the
procedure; the first line of the procedure will be highlighted in yellow, as in
Figure 2-22).
Add a breakpoint as described in the following section, then run the Sub
procedure in the usual way.
When the code window is displayed, with a line of code highlighted, you can
step through the code by pressing F8 or by using the Step Into toolbutton % .
The Step Into toolbutton is on the Debug toolbar; choose Toolbars from the
View menu and Debug from the submenu to display the Debug toolbar (Figure
2-23).
The highlighted line of code is the statement to be executed next.
Figure 2-23. The VBA Debug toolbar.
Adding a Breakpoint
A breakpoint allows you to halt execution at a specified line of code, rather
than having to step through the code from the beginning. There are several ways
to add a breakpoint:
Opposite the line of code where you want to set the breakpoint, click
in the gray bar on the left side of the VBA module sheet. The line of
code will be highlighted (usually in red-brown) and a breakpoint
indicator, a large dot of the same color, will be placed in the margin
(see Figure 2-24).
Place the cursor in the line of code where you want to set a breakpoint.
Press the Toggle Breakpoint button a on the Debug toolbar.