Tip If you put the insertion point anywhere on the variable you want to watch and add the watch the variable name will automatically be inserted in the Expression box for you

6. Click OK to close the Add Watch dialog box.

7. If not already open, open the Watch window in the VBE by clicking View > Watch Window. Figure 7-36 shows the Watch window with our expression added.



|| Expression

| Value

1 Type

| Context


¡Ja cell .Value s 30

<Out of context>



Figure 7-36. Watch expression added to Watch window

Figure 7-36. Watch expression added to Watch window

Notice that the value is currently out of context because the code is not yet running.

8. Run the AddSalesTotal macro from Worksheetl in Excel. The code enters break mode in the For Each...Next loop, and the Watch window shows us that the value of our condition is True, as shown in Figure 7-37.

Figure 7-37. Watch window showing the true condition with the code in break mode

9. Press F5 to continue running the code. Any time the watch encounters a cell value of less than 30, the code will enter break mode.

10. Press F5 until the code runs to completion.

Now that we've seen how to debug and troubleshoot our code, let's take a look at how to prevent errors from occurring and how to graciously notify users of errors.

0 0

Post a comment