Microsoft Office Tutorials and References
In Depth Information
You can also enter break mode based on the value of variables in your program. For example, you
could specify that the program enters break mode when the variable Count has the value 100 .
You learn about this technique in the next section, “Using Watches.”
When you are finished working in break mode, press F5 to continue macro execution. Other options
for controlling program execution are covered in the section “Controlling Program Execution” later in
While you are debugging a macro you can use watch expressions to keep track of the data your
macro is working with. A watch can be any expression — variables, properties, functions, and so
on. Because many program bugs are caused by variables and properties taking on unexpected
values, the use of watches is an important debugging tool.
VBA supports two types of watches and you can choose the one that’s right for your needs. Regular
watches are displayed in the Watches window, which you open by selecting Watch Window from
the View menu. You can have multiple watch expressions in the window, and VBA displays the
following for each one:
n Value: The current value of the watch expression.
n Type: The data type of the watch expression.
n Context: The names of the module and macro where execution is paused.
The Watches window is shown in Figure 22.9. It indicates the variable Count is type Integer and
has the value 45 , and the variable temp is type String as has the value “Week of 12/12/2006” .
Use the Watches window to view the value of variables and expressions during macro debugging.
A watch expression can be a single variable or an expression made up of variables and any of VBA’s
operators and functions. You would create watch expressions when you are interested in the
relationship between variables rather than their exact values. For instance, you might need to keep
track of whether the variable Count is greater than the variable Max without regard to their actual
values. The watch expression Count >Max displays a result of either True or False depending
on the data. Likewise, if you want to see whether a string’s length is less than 10, you would use
the watch expression Len(SomeString)<10 .