Debug.Print statements, on the other hand, only appear in the Immediate window. Although it??™s wise
to remove or comment them, your users won??™t see the output.
Debug.Assert
Assertions are another tool you can use to check for conditions within your code. Assertions
are conditional statements that you create that will put your code in break mode if the conditions
are not met. Listing 7-5 shows a sample method you can use as an example.
Listing 7-5. Sample Subroutine Using Debug.Assert
Sub TestAssert()
Dim iTest As Integer
iTest = 10
Debug.Assert iTest = 9
Debug.Print "Test Value: " & iTest
End Sub
1. Copy the code in Listing 7-5 into Standard Module1 in the DebugExample01.xlsm file.
2. Open the Immediate window by choosing View ?¤ Immediate Window or by pressing
the Ctrl+G shortcut keys.
3. In the Immediate window, type TestAssert.
4. Press Enter.
Our assertion is testing to see if the iTest variable equals 9. Since we set that variable to
10 in the line of code preceding the assertion, it will return a value of False and put the code
in break mode. Figure 7-13 shows the code in break mode as it stops on the assertion.
Of course, we forced this false condition, but if we need to know when a condition other
than what we expect might happen when testing our code, assertions provide us with that
option.
5. Press F5 to continue running the code to its end.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 260
Figure 7-13. A false condition in the Assert method puts the code in break mode.
Pages:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249