How to use the Excel VBA Watch Window

Below, you can see how the watch window monitors the variable value in Excel VBA.

Excel Watch Window


How to Add a Watch Window 

  • Open the VBA code editor window.
  • Go to View > Watch Window.

VBA watch window inserting

The watch window is displayed at the bottom of the VBA code editor window.

The watch window in the bottom of the window


How to Use Watch Expressions in Excel

  • Right-click the watch window and click Add Watch.

Adding the watch in the watch window

  • The Add Watch… window is displayed.
  • Enter the Variable you want to track in Expression
  • Select Watch Expression in Watch Type.
  • Click OK.

Mention the variable name to watch in the Expression box.

Read More: How to Monitor Cells Using Excel Watch Window


How to Use a Watch Window in Excel

Below, is a sample watch window:

Example of dataset

Get the summation of prices of product A and  B.

Monitor the summation of the product values and the summation of the total price of products per month.

  • Use the VBA code below.
Sub CalculateTotalSales()
Dim ws As Worksheet
Dim lastRow As Long
Dim totalSales As Double
Dim productA As Variant
Dim productB As Variant
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
For i = 5 To lastRow
If IsNumeric(ws.Cells(i, 3).Value) Then
productA = ws.Cells(i, 3).Value
Else
productA = 0
End If
If IsNumeric(ws.Cells(i, 4).Value) Then
productB = ws.Cells(i, 4).Value
Else
productB = 0
End If
totalSales = productA + productB
ws.Cells(i, 5).Value = totalSales
Next i
End Sub

It sums the values and presents the summation value in the next column.

Each time the totalSale variable value changes the watch window is updated.

  • Add a watch window to add a watch variable.

Adding watch to the VBA code editor

  • In that Add Watch window, mention the variable name.
  • Select Break When Value Changes.
  • Click OK.

Adding watch to the VBA code editor

The totalSales variable is added to the variable list.

  • If you try to run the code, you will see the variable totalSales is enlisted for tracking and updates.
  • Running the code updates the variable value.

Value flactuating after adding the watch first

This is the value of totalSales after the second iteration.

Second iteration of the values after presssing the Run button

This is the value of totalSales after the code is Run.

Final iteration after all the run sequence done

 

Read More: How to Trace Errors in Excel


What Are the Benefits of Using a Watch Window?

Real-time monitoring

It helps understand how the variables change throughout the execution and identify unexpected behaviors.

Simplified Debugging

You can quickly pinpoint the source of issues and address them more efficiently.

Minimizes Repetitive Checks

It provides a central location to monitor multiple variables simultaneously.

No Code Modification

It allows you to inspect variables without altering the original code.

Supports Complex Data Structures

It can handle complex data structures, such as arrays, objects, or multi-dimensional arrays. You can expand it and inspect nested elements.

Pausing Code Execution

You can pause the  VBA code execution and check the values of variables.

Efficient Tracking

It helps track progress and values of variables at different stages.

Variable Manipulation

You can change the value of a variable directly in the Watch Window while the code is paused.


Things to Remember

Variable scope: The Watch Window can only monitor variables that are in the current scope of the code.

Pausing code execution: To observe variable values accurately, you may need to pause code execution (using breakpoints or stepping through the code). Resume code execution after observing the variables.

Watch expressions: You can watch not only single variables but also expressions.

Updating the Watch Window: The values in the Watch Window update when the code is paused only. If you want to see updated values during th  execution, you need to manually pause the code.

Memory usage: Adding multiple variables or complex expressions to the Watch Window can increase memory usage and slow down the debugging process.

Watch Window Limitations: It can display a limited number of variables simultaneously.

Tracking object properties: You can expand the object’s properties in the Watch Window. However, not all object properties are readable or  update in real-time.

Datasets: Large datasets can cause the Watch Window to slow down or become unresponsive.

Floating-point accuracy: Be aware of potential floating-point precision issues, especially when comparing or calculating double or single-precision floating-point variables.

Disable Watches: Once you have finished debugging, consider disabling or removing unnecessary watches.


Frequently Asked Questions

Q1. What is the shortcut for the watch window in VBA?

CTRL + SHIFT + W.

Q2. How do I activate a window in VBA?

Use the Activate method of the window object:

WindowObject.Activate

Replace WindowObject with the name of the window you want to activate.

Q3. What is the watch window function in Excel?

The Watch Window is a debugging tool available in the Visual Basic for Applications (VBA) editor.


Download Practice Workbook

Download the following workbook.


<< Go Back to Auditing FormulasExcel Formulas | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Rubayed Razib Suprov
Rubayed Razib Suprov

Rubayed Razib, holding a BSC degree in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, serves as a devoted member of the ExcelDemy project. He has contributed significantly by authoring numerous articles and showcasing proficiency in VBA. Razib efficiently automates Excel challenges using VBA macros and actively participates in the ExcelDemy forum, providing valuable solutions for user interface challenges. Apart from creating Excel tutorials, he is interested in Data Analysis with MS Excel,... Read Full Bio

2 Comments
  1. All is fairly well presented except for no mention of displaying Public Variables which are supposed to be active in ALL procedures and modules. I put a Public Variable in to display in all procedures and I get an ‘out of scope’ kind of report. I was hoping for a thorough explanation.

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Mar 13, 2024 at 11:02 AM

      Hello Peter Clarke

      Thanks for reaching out and sharing your problems and suggestions. You put a Public Variable to display in all procedures but got an “out of scope” report. You want a thorough explanation.

      This issue could arise due to several reasons:

      • Proper Declaration: Ensure the Public Variable is declared correctly at the module level, typically at the top of a module and outside any specific procedure.
      • Module Inclusion: Check that the module containing the Public Variable is included in the project. If it’s not included or there are visibility issues, the variable may not be accessible across all procedures.
      • Reference Check: If multiple modules or projects exist, ensure no conflicts or reference issues. Public Variables should be accessible across all referenced projects and modules.

      I have demonstrated watching a Public Variable in the Watch Window. I am using three modules: one for the public variable, one for displaying the public variable, and one for watching the public variable.

      VBA Code in Module1:

      Public totalSales As Long

      VBA Code in Module2:

      Public Sub DisplayTotalSales()
          Debug.Print totalSales
      End Sub

      VBA Code in Module3:

      Sub CalculateTotalSales()
          
          Dim ws As Worksheet
          Dim lastRow As Long
          Dim i As Long
          Dim productA As Variant
          Dim productB As Variant
          
          Set ws = ThisWorkbook.Sheets("Sheet1")
          lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
          
          For i = 6 To lastRow
              
              If IsNumeric(ws.Cells(i, 3).Value) Then
                  productA = ws.Cells(i, 3).Value
              Else
                  productA = 0
              End If
              
              If IsNumeric(ws.Cells(i, 4).Value) Then
                  productB = ws.Cells(i, 4).Value
              Else
                  productB = 0
              End If
              
              totalSales = productA + productB
              ws.Cells(i, 5).Value = totalSales
              
              Call DisplayTotalSales
          
          Next i
      
      End Sub

      OUTPUT Overview:

      Hopefully, the idea will overcome your situation. Good luck.

      Regards

      Lutfor Rahman Shimanto
      Excel & VBA Developer
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo