Stepping Through Code

Assertions are great at stopping code if a condition is false, but we need a method to move back and forth through our code if we are to find out why our condition failed. The VBE has commands we can use to move through our code line by line, and to move directly to a line of code as well. Figure 7-14 shows the step section of the Debug menu.

Step Into Step Over SJ Step Out * Run To Cursor

Shift-t-FS Ctrl+Shrft+F8 Ctrl-t-FB

Figure 7-14. Step-through-code options from the Debug menu

Earlier in this chapter, Table 7-1 gave a quick overview of these functions. Now we'll take a closer look at them.

In Standard Module1 in the DebugExampleOl.xlsm file, add the code from Listing 7-6.

Listing 7-6. Sample Code for Debugging

Function TestLoop() As Long

Dim i As Integer

Dim lngResult As Long lngResult = 1 For i = 1 To 10 lngResult = lngResult * i ExternalProcess lngResult Next i

TestLoop = lngResult End Function

Sub ExternalProcess(TheValue As Long) If TheValue > 10000 Then

TheValue = 0 End If

End Sub

We're going to use this code to explore the various ways we can step through our code line by line, and we'll also see how to make use of additional tools like the Immediate window and the call stack.

The code in Listing 7-6 is a simulation of a construct we see often in our code. In the TestLoop function, we have a loop that runs a fixed number of times (in real-life situations, this may or may not be the case) and returns a value at the end of the process. Within that loop, we call a procedure to act on or process data or perform an action based on the value of variables in the loop. In this case, we have the appropriately named ExternalProcess subroutine.

Our TestLoop function is designed to multiply a value by the loop counter; the expected result of our process is the value 3628800. The ExternalFunction procedure is purposely coded to give an incorrect result. Let's run the code.

2. Show the Immediate window by choosing View > Immediate Window or by pressing Ctrl+G.

3. In the Immediate Window, type ?testloop. The result is shown in Figure 7-15.

j<]|

?testloop

-

Ü

<1 1

Figure 7-15. The TestLoop function returns an incorrect value.

Let's begin debugging by inserting a breakpoint in our code.

1. In the code window, find the TestLoop function.

2. Move the mouse pointer to the gray left margin next to the start of the For...Next loop, and click. A maroon break marker is inserted, as shown in Figure 7-16.

Figure 7-16. Breakpoint inserted

3. In the Immediate window, run the code again by typing TestLoop and pressing Enter.

4. The code runs as far as the beginning of the loop, and then stops, as shown in Figure 7-17.

Once you're in break mode, the break line text (the code at the breakpoint) and the execution line text (the currently executing line of code as you step through) are highlighted. The default highlight color is maroon for the break line text and yellow for the currently executing line of text. This color coding is user-definable on the Tools > Options menu on the Editor Format tab, as shown in Figure 7-18.

At the first breakpoint, you will get a combination of the break and execution line colors.

Figure 7-17. VBE enters break mode when a breakpoint is reached.
Figure 7-18. Options dialog box showing VBE color options

5. The Step Into command moves you through your code one line at a time. Press the F8 key (or choose Debug > Step Into) to move to the first line of code inside the loop. The color coding should now be more easily seen, as in Figure 7-19.

Figure 7-19. Better view of the break and execution lines of code

6. Press the F8 key twice to move the execution line to the ExternalProcess routine.

7. Press F8 again to move the execution line into the ExternalProcess procedure.

As you can see, the Step Into command moves you through the code one line at time. When it encounters another procedure call, it moves you into that procedure. At the moment, our execution point is inside the ExternalProcess subroutine. To move out of this procedure without moving line by line, use the Step Out command by choosing Debug > Step Out or by pressing Ctrl+Shift+F8. The Step Out command moves you out of the current procedure and to the next line of code in the procedure that called the current procedure.

8. Press Ctrl+Shift+F8 to move the execution point back into the TestLoop function at the end of the loop structure, as shown in Figure 7-20.

Function TestLoopO As Long

Dim i As Integer

Dim IngResult As Long

IngResult = 1

IngResult = IngResult * i ExternalProcess IngResult | Next i

TestLoop = IngResult End Function

Figure 7-20. Result of Step Out command

9. Press F8 two times. The execution point should be back on the call to ExternalProcess, as shown in Figure 7-21.

If you don't have a need to step through this external procedure and want to skip over this call, you can use the Step Over command.

10. Choose Debug > Step Over or press Shift+F8.

11. The execution point moves to the bottom of the loop without stepping through the procedure call, as shown in Figure 7-22.

Function TestLoop () As Long

Dim i As Integer

Dim IngResult As Long

IngResult = 1

IngResult = IngResult * i ExternalProcess IngResult Next i

TestLoop = IngResult End Function

Figure 7-21. Execution point on a procedure call

Function TestLoop() As Long

Dim i As Integer

Dim IngResult As Long

IngResult = 1

IngResult = IngResult * i ExternalProcess IngResult Next i

TestLoop = IngResult End Function

Figure 7-22. The execution point moves directly to the next line of code when the Step Over command is used.

What if you decide that you should have stepped into the procedure you just stepped over? (In a loop like this, you may need to test that procedure with the current counter variable in place.) The VBE gives us a command called Set Next Statement that makes this very easy to do.

To bring the execution point back to the call to ExternalProcess, do the following:

12. Put the cursor on the line of the next statement that you want executed (in this case, the call to ExternalProcess).

13. Choose Debug > Set Next Statement or press Ctrl+F9.

The result is shown in Figure 7-23.

Function TestLoop() As Long

Dim i As Integer

Dim IngResult As Long

IngResult = 1

IngResult = IngResult * i ExternalProcess IngResult Next i

TestLoop = IngResult End Function

Figure 7-23. The Set Next Statement command moves the execution point to the selected line of code.

The Set Next Statement command can be used to move the execution point forward as well as backward. You can also use the mouse to drag the execution arrow (shown in the gray margin on the left side of the code window in Figure 7-23) to the location of the code you'd like to run.

0 0

Post a comment