Adding a Watch Expression

Many errors in procedures are caused by variables that assume unexpected values. If a procedure uses a variable whose value changes in various locations, you may want to stop the procedure and check the current value of that variable. Visual Basic offers a special Watch window, which allows you to keep an eye on variables or expressions while your procedure is running. To add a watch expression to your procedure, perform the following:

1. In the Code window, select the variable whose value you want to monitor.

2. Choose Debug | Add Watch.

The screen will display the Add Watch dialog box, as shown in Figure 13-9.

Figure 13-9:

The Add Watch dialog box allows you to define conditions that you want to monitor while a VBA procedure is running.

j Microsoft Visual Basic - Chdpl3.»ls - [Breaks (Lode))

^ File Edit View Insert Format Debug Run Took Add-Ins Window Help _ d? x a ia - b | * fe e «|« « ► ii ■ & nf * 0 i s !r* mlL ** jCBf

Projet! Debugging

Microsoft re I Objects fc] Sheet 1 (Sheet 1) ŒQ 5he«2 (She«2) sheets (sheets) ■■ S ItiisWorkbook E~gä Modules

¡Properties - Breaks

E3

1 Breaks Module

J

Alphabetic | Categorized |

|BB!Bl Breaks

[(General) [siopExample T i

Sub StopExarripleQ Dim curCel As Range Dim nurn As Integer

Acl iveWoik boo k. S he ets(1). S ele cl Acl iveS h est. U s ed Ran ge. Select num = Selection. Columns. Count Selection.Resize(1, nutn). Select

Foi Each curCell In Selection

Debug.Print curCelLTexl Next

End Sub_

Figure 13-9:

The Add Watch dialog box allows you to define conditions that you want to monitor while a VBA procedure is running.

The Add Watch dialog box contains three sections described in the following table:

Expression Displays the name of a variable that you have highlighted in your procedure. If you open the Add Watch dialog box without selecting a variable name, type the name of the variable you want to monitor in the Expression text box.

Context In this section you should indicate the name of the procedure that contains the variable and the name of the module where this procedure is located.

Watch Type Specifies how to monitor the variable. If you choose the Watch Expression option button, you will be able to read the value of the variable in the Watch window while in break mode. If you choose the Break When Value is True option button, Visual Basic will automatically stop the procedure when the variable evaluates to true (nonzero). The last option button, Break When Value Changes, stops the procedure each time the value of the variable or expression changes.

You can add a watch expression before running a procedure or after execution of your procedure has been suspended.

The difference between a breakpoint and a watch expression is that the breakpoint always stops a procedure in a specified location and the watch stops the procedure only when the specified condition (Break When Value is True or Break When Value Changes) is met. Watches are extremely useful when you are not sure where the variable is being changed. Instead of stepping through many lines of code to find out where the variable assumes the specified value, you can simply put a watch breakpoint on the variable and run your procedure as normal. Let's see how this works. 1. Prepare the procedure shown in Figure 13-10.

Figure 13-10: Using the Watches window

The WhatDate procedure uses the For.. .Next loop to calculate the date that is x days in the future. If you run this procedure, you won't get any result unless you insert the following instruction in the code of the procedure:

MsgBox "In " & x & " days, it will be " & NewDate

This time, however, you don't want to display the individual dates, day after day. Suppose that you want to stop the program when the value of the variable x reaches 211. In other words, you want to know what date will be 211 days from now. To get the answer, you could insert the following statement into your procedure:

If x = 211 Then MsgBox "In " & x & " days it will be " & NewDate

Let's say you want to get the answer without introducing any new statements into your procedure. How do you do this? If you add watch expressions to the procedure, Visual Basic will stop the For.. .Next loop when the specified condition is met, and you'll be able to check the values of the desired variables.

1. Choose Debug | Add Watch.

2. In the Expression text box, enter the following expression: x = 211.

3. In the Context section, choose What Date from the Procedure combo box, and Breaks from the Module combo box.

4. In the Watch Type section, select the Break When Value is True option button.

5. Click OK to close the Add Watch dialog box. You have now added your first watch expression.

6. In the Code window, position the insertion point anywhere within the name of the curDate variable.

7. Choose Debug | Add Watch, and click OK to set up the default watch type with Watch Expression.

8. In the Code window, position the insertion point anywhere within the name of the newDate variable.

9. Choose Debug | Add Watch, and click OK to set up the default watch type with Watch Expression.

After performing the above steps, the WhatDate procedure contains the following three watches: x = 211 Break When Value is True curDate Watch Expression newDate Watch Expression

10. Position the insertion point anywhere inside the code of the WhatDate procedure, and press F5. Visual Basic stops the procedure when x equals 211 (see Figure 13-10).

Notice that the value of the variable x in the Watches window is the same as the value that you specified in the Add Watch dialog. In addition, the Watches window shows the value of the variables curDate and newDate. The procedure is in break mode. You can press F5 to continue or you can ask another question: What date will be in 277 days? The next step shows how to do this.

11. Choose Debug | Edit Watch, and enter the following expression: x = 27. You can quickly display the Edit Watch dialog box by double-clicking the expression in the Watches window.

12. Click OK to close the Edit Watch dialog box. Notice that the Watches window now displays a new value of the expression. x is now False.

13. Press F5. The procedure stops again when the value of x equals 277. The value of curDate is the same. However, the newDate variable now contains a new value—a date that is 277 days from now. You can change the value of the expression again or finish the procedure.

14. Press F5 to finish the procedure without stopping.

When your procedure is running and a watch expression has a value, the Watches window displays the value of the watch expression. If you open the Watches window after the procedure has finished, you will see <out of context> instead of the variable values. In other words, when the watch expression is out of context, it does not have a value.

Removing Watch Expressions

1. In the Watches window, click on the expression you want to remove and press Delete. Remove all the watch expressions you defined in the preceding exercise.

0 0

Post a comment