Debug Print statements on the other hand only appear in the Immediate window Although its wise to remove or comment them your users wont see the output


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 Modulel 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.

Figure 7-13. A false condition in the Assert method puts the code in break mode.
0 0

Post a comment