Using Watch Expressions Debug A Procedure

You can use watches to monitor the value of specific expressions or variables as you step through the VBA code in a procedure. VBA code watches work well for debugging code to determine why a variable or expression does not return the anticipated value. Programmers commonly refer to this type of error as a logical error. When you set watches, the Visual Basic Editor displays each watch along with the value of the specified expression or variable in the Watches window.

The Watches window, which displays in the same location as you last viewed it, lists the active watches along with the current value of the expression or variable being watched. If the selected procedure is not running or is in break mode, the expression has a value of <Out of context>. See Chapter 2 for more information on displaying windows in the Visual Basic Editor.

You set a watch expression using the Add Watch dialog box, where you specify the expression that you want to watch in the Expression field. Typically the expression you specify checks for the value of a variable to meet specific criteria, as in the example n > 8.

After you specify the expression to watch, you must select an option to specify the type of watch to perform. When you select the Watch Expression option, the value of the expression displays in the Watches window as the procedure executes. The value of the expression is always a Boolean value of true or false to indicate whether the expression is true. The Break When Value is True option instructs VBA to break execution of the procedure as soon as the condition is true, whereas the Break When Value Changes option breaks the execution of the procedure as soon as the value of the expression changes from true to false, or vice versa.

USING WATCH EXPRESSIONS TO DEBUG A PROCEDURE

USING WATCH EXPRESSIONS TO DEBUG A PROCEDURE

'—n In the Projects window, open the module containing the procedure you want to debug.

■ The Watches window displays in the last viewed location.

< Click Debug O Add Watch.

'—n In the Projects window, open the module containing the procedure you want to debug.

Note: See Chapter 2 for information on opening VBA modules.

L-0 Click View O Watch Window.

■ The Watches window displays in the last viewed location.

< Click Debug O Add Watch.

0 0

Responses

  • andreas
    What is a watch expression in vba?
    7 years ago

Post a comment