Getting Started with the Watch Window

White and blue eye state in front of a building - Photo by Matthew T Rader on Unsplash
Photo by Matthew T Rader on Unsplash
The Excel VBA coding dojo, the VBA Editor, has many useful windows. Here, you'll explore its Watch window which lets you statically track variable values

In this article:

8 minutes read

This article is part of the Visual Tour of Excel’s Coding Dojo series, exploring each of the Excel VBA Editor’s menus, toolbars, and windows. Here, we’ll take a close look at the Excel VBA editor’s Watch window.

“Perfectionism is a disease. Procrastination is a disease. ACTION is the cure.”

Richie Norton

If you wish to skip to any of the other posts in the series, click on any of the topics listed below:

Meet your Mat! The VBA Editor

Most great martial artists first had to get intimate with their dojo. Likewise, to master Excel VBA, you’ll have to get comfy loitering in its coding dojo, the VBA Editor. Naturally, the first step is to open the VBA editor.

“The Dojo is the only place you can die and come back to life, so use it.”

kyoshi Larry Mabson (American karateka)

I, for one, know how daunting the Excel VBA editor can appear to beginners – just take a look at the overview below! However, looks can be deceiving; at least that’s true here. I say that because anyone familiar with programming IDEs will tell you that the VBA editor is one of the more user-friendly IDEs out there.

IDE stands for Integrated Development Environment. They are software that consolidates application development tools.

Laying out the different areas and windows of the Excel VBA Editor (VBE)
Laying out the different areas and windows of the Excel VBA Editor (VBE)

Most developers only use a few of the VBA Editor’s menus, toolbars, and windows. As a result, they miss out on several tools that would make their work less stressful and enhance their productivity.

But you are not most developers, are you? For one thing, you are here, so I take it you would like to max out your VBA programming potential. Brilliant! You are well on your way there.

Watch Window – Overview

Like the Locals and Immediate windows, the Watch window opens below the Code window. However, you can drag and place it as required. Also, the Watch window is hidden by default.

To display the Watch window, you select it from the View menu or click its icon on the Debug toolbar. Moreover, the “Alt + V + H” keyboard shortcut also displays the Watch window.

Illustrating the multiple ways of displaying the Watch window
Illustrating the multiple ways of displaying the Watch window

How the Watch window differs from the Locals window

The Watch window, (see the image below) is identical to the Locals window, except that:

  • It lists only developer-selected variables or expressions, not the entire scope-local variables;
  • It shows an extra column for those selected variables’ or expressions’ Context. Here, Context refers to the module or procedure where variables’ values are evaluated;
  • It does not include a Calls Stack button.
The Excel VBA Editor’s Watch window
The Excel VBA Editor’s Watch window

The Call Stack is a queue of all procedures scheduled for execution in the current scope. Often, you’ll link your procedures using Call statements. So, completing a task will require running a series of procedures (think of them as sub-tasks). The Call Stack lets you can see this queue of procedures.

So, the Watch window really lets you focus your attention on the values stored in specific variables or results of certain expressions.

Anatomy of the Watch Window

Each developer-selected variable or expression is called a Watch. That’s probably why the Watch window is titled Watches even though its label is Watch (see the preceding image).

Like the Locals window, the Watch window does its work while unhidden, and the editor enters Break mode. As such, its primary use is code testing and debugging, as indicated by its icon’s location on the Debug toolbar.

The VBA Editor works in any one of three modes – Design, Run (a.k.a. Runtime) and Break modes. These modes complement the primary development stages – coding and GUI (graphic user interface) design, running/testing, and debugging.

Once in Break mode, the Watches appear in the Watch window to allow tracking or editing of their values. Also, regarding the image above, note the following:

  • Dragging column borders resize them;
  • Clicking the Close box (at the top right – double-click the window’s title bar if it is missing) hides the window;
  • The Watch window automatically pops-up when you add Watches.

Adding a New Watch to the Watch Window

To add a Watch, highlight the variable or expression in the Code window, then:

  • Select Add Watch from the Debug menu, or right-click the highlighted variable or expression and pick Add Watch from the pop-up context menu. Then fill the Add Watch dialog box (see the image below). In the Debug menu option, highlighting the variable or expression is not mandatory. However, in that case, the variable or expression must be typed in the dialog box instead of being auto-filled. So, the Debug menu option without prior variable or expression highlighting is less efficient.  
Add a new Watch using the Add Watch dialog
Add a new Watch using the Add Watch dialog
  • Alternatively, drag and drop the highlighted variable or expression into the Watch window (see the image below). That will auto-set the new Watch’s context to the current procedure (i.e., the procedure from which it was dragged). However, changing the auto-set context requires editing the new Watch – which we’ll get to in a second.  
Add a new Watch using the Drag-and-Drop method
Add a new Watch using the Drag-and-Drop method
  • Otherwise, select Quick Watch from the Debug menu or toolbar, or press “Shift + F9“. Either option displays the Quick Watch dialog box (see the image below), click the Add button to finish.  
Add a new Watch using the Quick Watch dialog
Add a new Watch using the Quick Watch dialog

Editing one of the Watch Window’s Watches

To edit a Watch, select it in the Watch window and select Edit Watch from the Debug menu. Another way is to right-click on the Watch in the Watch window and pick Edit Watch from the pop-up context menu.

Either method displays the Edit Watch dialog box (see the image below). Change the Watch’s attributes as you desire, then click the OK button to finish.

Edit Watch dialog
Edit Watch dialog

Deleting one of the Watch Window’s Watches

To delete a Watch, first select it in the Watch window. Next, hit the Delete (Del) key or choose Clear from the Edit menu, or pick Edit Watch from the Debug menu and hit the Delete button. Alternatively, right-click the Watch and select Delete Watch from the context menu (see the image below).

Right-click a Watch for the context menu allowing its deletion. Watches can also be edited or added in this way
Right-click a Watch for the context menu allowing its deletion. Watches can also be edited or added in this way

The Long and Short of It

So, there you have it! Now, you know all you need to incorporate the Watch window into your development workflow. The Watch window lets you inspect the values (or results) stored in a selected number of variables (or expressions).

You can select variables (or expressions) in different procedures or modules. So, the Watch window’s contents (i.e., Watches) are independent of the current scope of program execution.

The window isn’t displayed by default in your VBA editor, so you’ll need to select it from the View menu or click its icon on the Debug toolbar, or press “Alt + V + H” keyboard shortcut.

You can also check out the Office Dev Center page for more info on the VBA editor’s Watch window.

As I mentioned earlier, this article is part of the Visual Tour of Excel’s Coding Dojo series exploring each of the Excel VBA Editor’s menus, toolbars, and windows. To check out any of the other posts in the series, click on topic links below:

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles