A Deeper Look
Let??™s take a look at some of the other tools we can use to make our debugging more efficient.
In this section, we??™ll look at the following:
??? Step-through options
??? The Immediate window
??? The Locals window
??? The Watch window
??? The call stack
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.
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.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 261
In Standard Module1 in the DebugExample01.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.
Pages:
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250