totn Excel 2016 VBA Debugging

MS Excel 2016: Add a Watch Expression in VBA

This Excel tutorial explains how to add a Watch expression in the VBA debugging environment in Excel 2016 (with screenshots and step-by-step instructions).

Adding a Watch Expression

In Excel 2016, the Watch Window displays the value of a watched expression in its current state. This can be extremely useful when debugging VBA code. Let's explore how to add an expression to the Watch Window.

To add a Watch expression, select Add Watch under the Debug menu.

Microsoft Excel

When the Add Watch window appears, enter the expression to watch and click the OK button when you are done.

Microsoft Excel

In this example, we've entered the following watch expression in the Expression field:

Len(pValue)

Next, we've selected AlphaNumeric as the Procedure and Module1 as the Module when setting up the Context for the watched expression.

Finally, we've selected Watch Expression as the Watch Type but there are 3 options to choose from:

Watch Type Description
Watch Expression To display the value of the watched expression in its current state
Break When Value Is True To stop the execution of the code when the value of the watched expression is True
Break When Value Changes To stop the execution of the code when the value of the watched expression changes

Now when you return to the VBA window, the Watch Window will automatically appear if it was previously hidden. Within the Watch Window, all of the watched expressions should be listed including the one that we just added.

Microsoft Excel

As you can see, the expression Len(pValue) now appears in the Watch Window with a value of 6. Adding a watch is a great way to keep track of variables or expressions of interest when debugging your VBA code.