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:
- How to Open the VBA Editor in Excel
- Exploring each VBA Editor Menu in Excel
- Exploring each VBA Editor Toolbar in Excel
- Getting Started with the Project Explorer
- Getting Started with the Properties Window
- Exploring the VBA Editor’s Code Window
- Exploring the VBA Editor’s Immediate Window
- Getting Started with the Locals Window
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.
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.
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 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.
- 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.
- 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.
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.
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).
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:
- How to Open the VBA Editor in Excel
- Exploring each VBA Editor Menu in Excel
- Exploring each VBA Editor Toolbar in Excel
- Getting Started with the Project Explorer
- Getting Started with the Properties Window
- Exploring the VBA Editor’s Code Window
- Exploring the VBA Editor’s Immediate Window
- Getting Started with the Locals Window