How to use the Immediate Window to debug variable values in real-time.

The Immediate Window is an interactive execution and evaluation console built directly into the Visual Basic Editor (VBE). It allows you to inspect variable values, evaluate expressions, test code syntax, and alter variable states in real-time while a macro is paused, eliminating the need to add temporary message boxes or guess why a calculation failed.

Quick Answer

Press Ctrl + G in the Visual Basic Editor to open the Immediate Window. To inspect any variable or expression, type a question mark followed by the variable name (for example, ? TargetRow or ? Range("A1").Value) and press Enter. The current value prints directly on the line below.


Setting Up Your Debugging State

The Immediate Window can run commands at any time, but it is most useful when code is actively executing in Break Mode. In Break Mode, execution pauses on a specific line of code, freezing all local and global variables in memory for inspection.

1. Set a Breakpoint

  1. In the VBA Editor, locate the routine you want to diagnose.
  2. Click the margin to the left of the code line where you want execution to pause, or place your cursor on the line and press F9.
  3. A dark red dot appears in the margin, and the line is highlighted in red.
  4. Run your macro (F5). Execution halts before the marked line runs, highlighting it in yellow.
Sub CalculateTax()
    Dim rate As Double, total As Double
    rate = 0.08
●   total = 100 * rate    <-- Execution pauses here in yellow
End Sub

To decide whether manual breakpoints or explicit code commands are best for your troubleshooting task, see The “Stop” statement vs. “Breakpoints”: When to use each.


Core Operations in the Immediate Window

Once execution is paused, press Ctrl + G to shift focus to the Immediate Window.

1. Printing Variable and Property Values (?)

The question mark (?) is VBA shorthand for Debug.Print. Type ? followed by what you want to check, then press Enter:

CommandActionOutput Example
? myVariablePrints the value currently held in memory450.75
? TypeName(myVariable)Returns the underlying data typeDouble
? ActiveCell.AddressReturns the active cell reference$B$12
? Range("A1").ValueReads the current grid value directlySubmitted
? Len(myString)Evaluates a function on a live variable14

2. Evaluating Complex Logical Conditions

If an If...Then block is not executing as expected, evaluate the logical test directly inside the Immediate Window:

? (RowCount > 100) And (HasErrors = False)
True

If this expression evaluates to False, evaluate each condition separately to isolate which half of your logic failed:

? RowCount > 100
True

? HasErrors = False
False

Here, you can see immediately that HasErrors is True, explaining why your conditional block did not trigger.

3. Overriding Variables in Real-Time

You can reassign variable values directly in the console without resetting or restarting the macro. This lets you test fixes immediately:

? total
0

total = 8.00

? total
8

After modifying the variable, press F8 to step forward one line at a time, or press F5 to resume normal execution. Excel continues running using your newly assigned value.

4. Executing Ad-Hoc Commands

You can execute standalone VBA actions without writing them into your module:

  • Close an open file: Workbooks("Temp.xlsx").Close False
  • Clear contents in a test range: Sheets(1).Range("A1:G50").ClearContents
  • Restore suppressed settings: Application.ScreenUpdating = True: Application.EnableEvents = True

Immediate Window vs. Other Debugging Tools

The Immediate Window works best alongside the other diagnostic tools in the VBE:

┌────────────────────────────────────────────────────────────────────────┐
│                        VBA Diagnostic Tools                            │
├──────────────────────┬─────────────────────────────────────────────────┤
│ Immediate Window     │ Ad-hoc queries, testing logic, changing values  │
├──────────────────────┼─────────────────────────────────────────────────┤
│ Debug.Print          │ Automated logging over long loops to console    │
├──────────────────────┼─────────────────────────────────────────────────┤
│ Watch Window         │ Continuous tracking of objects and break-on-change│
├──────────────────────┼─────────────────────────────────────────────────┤
│ Locals Window        │ Automatic display of all variables in scope     │
└──────────────────────┴─────────────────────────────────────────────────┘
  • When to use Debug.Print: Use Debug.Print when diagnosing massive loops where stopping at a breakpoint on every iteration is impractical. Statements in your code write results directly to the Immediate Window for review after the loop finishes. Learn more in Debug.Print: The developer’s best friend for silent error logging.
  • When to use the Watch Window: Use the Watch Window when you need execution to pause automatically whenever a variable changes value or hits a specific boundary condition. See Using Watch Windows to track Object states.

Diagnosing Silent Variable Failures

If you query a variable in the Immediate Window and it returns an empty string, 0, or Empty, but you expected a value:

  1. Check Variable Scope: A variable declared inside a specific subroutine (Dim x As Long) is only accessible in the Immediate Window while execution is paused inside that exact subroutine. Once the procedure ends, local variables are cleared from memory.
  2. Check for Typographical Errors: If you misspell a variable name (e.g., typing ? RowCont instead of ? RowCount), VBA dynamically creates a new, uninitialized Variant variable that evaluates to Empty.
  3. Enforce Explicit Declarations: Place Option Explicit at the very top of every module. This forces the compiler to flag undeclared variables before execution begins, preventing mistyped variables from returning false blanks during debugging. Review “Variable not defined”: Why you must use Option Explicit.

For runtimes where errors cause macros to terminate before you can open the console, route your routine through a central trap using Building a Global Error Handler: The Err.Number and Err.Description guide.