Back to Blog

VBA Immediate Window in Excel — the Console That Runs Code as You Ask

|

VBA Immediate Window in Excel — the Console That Runs Code as You Ask

TL;DR — The Immediate Window (open it with Ctrl+G) is where Debug.Print output lands and a live console where you can type one line of VBA and run it instantly. ? expr is shorthand for Print expr: it evaluates the expression and shows the result. You can read a value, change a value, call a Sub, or run a one-line loop — all without editing your code. Two things to know: a ? query actually runs the code (side effects included), and your local variables are only alive while the macro is paused at a breakpoint.

? Range("A1").Value               ' read a value  (? is short for Print)
Range("A1").Value = 42            ' write a value, live
? ActiveSheet.Name                ' inspect the environment
? Cells(Rows.Count, 1).End(xlUp).Row   ' test an expression before you commit it
MyMacro                           ' call a Sub by name, right now
For i = 1 To 3 : ? i * i : Next   ' a one-line loop: 1  4  9

What you'll learn

  • The mental model — the Immediate Window is an interactive console, not just a log
  • The three observers of rising power, and where this one sits
  • What the ? shortcut really does, and why it runs your code
  • Why your local variables read as empty until the macro is paused
  • How to call a Sub, change state, and loop by hand from one line
  • When to stop poking the console and put the logic in a module

The mental model: a live console, not just a log

Most people meet the Immediate Window as the place Debug.Print writes to. That is half of it. The other half is that you can type into it — any single line of VBA — and it executes the instant you press Enter, using the live state of Excel and your paused macro. It is a REPL: a read-eval-print loop bolted onto your workbook.

So the right question in your head is not "where did my Debug.Print go?" — it is "what do I want to ask or change right now?" Debug.Print is a line you wrote earlier that runs when the code runs; the Immediate Window is a line you type this second. That difference — scripted-ahead versus interactive — is the whole reason to reach for it: it is the fastest way to test a hunch without touching your source.

The three observers, of rising power

The Immediate Window is the middle rung of a ladder. When a macro misbehaves, you are not short of clues, you are short of a way to ask. VBA gives you three, each answering a different question:

Observer The question it answers Its signature lie
Debug.Print What were the values as it ran? Prints to a window closed by default
Immediate Window What is true right now, at this pause? A ? query actually runs the code
Breakpoint + F8 Which line does it go wrong on? They vanish when you close the workbook

Debug.Print shows you the past; a breakpoint freezes the present line by line. The Immediate Window sits between them: it lets you interrogate the present. Pause a macro on a breakpoint, then type questions at it — ? total, ? cells(r, 1).Value, ? Selection.Address — and get answers from the exact state your code is in. That interactivity is its power and, as the next section shows, its trap.

Trap 1: the ? query actually runs the code

? expr does not "preview" the expression — it evaluates it. For a plain value read that is exactly what you want. For anything with side effects, "just looking" is real execution:

? Range("A1").Value     ' safe - reads a value
? DeleteOldRows()       ' NOT safe - this runs DeleteOldRows and deletes rows
DeleteOldRows           ' also runs it - a bare Sub name executes

The console has no dry-run mode. Typing ? SomeFunction() to "see what it returns" runs the function, commits its writes, moves the cursor, saves the file — whatever it does. Keep your ad-hoc queries to pure reads (.Value, .Address, .Count, a variable) and never fish a side-effecting function through ? just to inspect its result. If you need the return value of something that also mutates state, you already have a design worth pausing on.

Trap 2: local variables are empty unless the macro is paused

This is the one that makes people think the Immediate Window is broken. Type ? myTotal when nothing is running and you get 0 or a blank line — not the value you saw a moment ago.

Sub Calc()
    Dim myTotal As Double
    myTotal = 1234.5
    Stop                 ' pause here, THEN ? myTotal shows 1234.5
End Sub
' Type  ? myTotal  with no macro paused -> empty. The local no longer exists.

A variable declared with Dim inside a Sub only exists while that Sub is running, and you can only read it while the macro is paused in break mode inside that Sub — at a breakpoint or a Stop. Once the Sub ends (or before it starts), the local is gone, so the console has nothing to show. Two ways around it: pause the macro on a breakpoint and query while it holds, or promote the variable to module level (declare it at the top of the module), where it persists between runs and the Immediate Window can always read it.

Trap 3: unqualified references run against whatever is active

A line you type into the console has no host Sub, so bare references resolve against the active object, which may not be the one you are thinking about:

? Range("A1").Value            ' reads A1 of the ACTIVE sheet - which one is that?
? ThisWorkbook.Sheets("Data").Range("A1").Value   ' unambiguous

If you switched to a different sheet to look at your data, Range("A1") now reads that sheet. When the answer surprises you, qualify the reference fully with the workbook and sheet before you conclude your macro is wrong.

Immediate Window vs Debug.Print

Same window, opposite workflows:

Immediate Window Debug.Print
How you use it Type a line, it runs now Written into the code, runs when the code runs
Best for One-off "what is X right now?" Repeated logging you planned ahead
Interactive? Yes — read, write, call, loop No — it only emits
Survives the run? No, it is live and ad-hoc The lines stay in your source

Use Debug.Print when you know in advance which values you want to watch every time the macro runs. Use the Immediate Window when you are mid-investigation and want to ask a question you did not anticipate — and to run tiny experiments: call a Sub with MySub arg1, arg2 (no parentheses) or Call MySub(arg1, arg2), reformat a cell, or check ? Application.WorksheetFunction.Sum(Range("B:B")) before you bake it into a formula.

The opinion: it is a console, not an editor

The Immediate Window is the fastest hypothesis tester in the VBA editor, and the ? prefix — just an abbreviation for Print — is the most useful keystroke most people never learn. Lean on it hard for probing: check an assumption, poke a value, confirm a Range resolves the way you expect before you write ten lines that depend on it.

But it is a console, and console work is disposable. Anything you type that turns out to matter belongs in a module, not in a one-line experiment you will lose the moment you clear the pane. And treat ? as a loaded gun: in a place where evaluating is executing, make "only read here" a habit, so an idle "let me just check what this returns" never quietly deletes a hundred rows.

When the whole job is checking your logic — describe it instead

Half the time the console work is really one question asked over and over — "does this rule hold for every row?" Poking ? Cells(r, 5).Value one cell at a time answers it for row r and no others, and you are back to guessing which r matters. ExcelMaster lets you ask the whole-column version in plain English — "show me every row where the margin is below 10 percent" — and it writes Python that reads the sheet, backs up the file first, evaluates the rule across all the data, and returns the rows that fail. The Immediate Window checks one value at a time; describe the check and get the whole answer at once.

Frequently asked questions

What is the Immediate Window in VBA?

It is an interactive pane in the VBA editor (open with Ctrl+G) that does two jobs: it displays the output of Debug.Print, and it acts as a live console where you can type a single line of VBA and run it immediately against the current state of Excel and any paused macro.

What does the ? mean in the Immediate Window?

? is shorthand for the Print statement. ? expr evaluates the expression and prints the result, so ? Range("A1").Value prints the value in A1 and ? 6 * 7 prints 42. It is the quickest way to inspect anything while you work.

Why can't I see my variable's value in the Immediate Window?

A variable declared with Dim inside a Sub only exists while that Sub runs, and you can read it only while the macro is paused in break mode inside that Sub. If nothing is paused, ? myVar shows nothing. Set a breakpoint and pause there, or declare the variable at module level so it persists.

Can I run a macro from the Immediate Window?

Yes. Type the Sub's name and press Enter. For a Sub that takes arguments, use MySub arg1, arg2 without parentheses, or Call MySub(arg1, arg2) with them. Functions run the same way, and ? MyFunction(3) prints the return value.

How do I clear the Immediate Window?

Click inside it, press Ctrl+A to select all, then Delete. There is no VBA command to clear it programmatically — it is a developer pane, not something your macro controls at runtime.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-05.

Related guides: VBA Debug.Print · VBA Breakpoint · VBA MsgBox · VBA Sub · VBA Dim