SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 239 | Next

Jim DeMarco

"Pro Excel 2007 VBA"


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.
1. Open the VBE.
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.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 262
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.


Pages:
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251