Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Debug Print: How to Use the Immediate Window

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Debug.Print statement in Excel VBA writes text to the Immediate Window in the Visual Basic Editor (VBE), so you can watch values, loop counters and messages while your code runs without opening a message box. To use it, open the VBE with Alt+F11, make sure the Immediate Window is visible (Ctrl+G), add lines like Debug.Print myValue to your code, and run the macro; the output appears at the bottom of the editor.

Debug.Print is one of the simplest debugging tools in VBA, and one of the most useful. Because it does not interrupt execution the way MsgBox does, you can log dozens of values inside a loop and inspect them afterwards, or leave logging lines in place behind a flag and switch them on when something looks wrong. The exact wording of menus and buttons in the Visual Basic Editor can differ slightly by Excel version, so if a command is not where you expect it, look for the closest equivalent.

What You'll Need

  • Excel on Windows with macros enabled. The Visual Basic Editor is available in the desktop version of Excel. Excel for the web does not include the VBE, so you cannot run or debug VBA there.
  • A workbook saved in a macro-enabled format (.xlsm) if you want to keep the code. A standard .xlsx file discards macros when saved.
  • A macro or a blank module to experiment in. If you have none yet, you can add one in a few seconds from the editor.
  • Basic familiarity with writing a line or two of VBA, such as assigning a value to a variable.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel on Windows, press Alt+F11. (On a Mac keyboard the shortcut is usually Fn+Option+F11, depending on your keyboard settings.) The editor opens in its own window.
  2. Show the Immediate Window. Press Ctrl+G, or use the View menu and choose Immediate Window. If the window is already visible at the bottom of the editor, the command toggles it. This is the pane where Debug.Print output appears.
  3. Insert a module if you need one. In the Project Explorer on the left, right-click your workbook name (it appears as VBAProject followed by the file name), choose Insert, then Module. A blank code pane opens. If your macro already lives in a standard module, you can skip this.
  4. Write a simple test procedure. Type something like the following, pressing Enter at the end of each line:
    Sub TestPrint()
    Dim i As Long
    For i = 1 To 5
    Debug.Print "Loop " & i
    Next i
    End Sub
    The ampersand joins the text and the number into one string. Debug.Print accepts any expression, so you can print numbers, text, cell values or the result of a calculation.
  5. Run the macro. Click anywhere inside the procedure and press F5, or use Run and then Run Sub/UserForm. If a dialog asks which macro to run, select TestPrint. The Immediate Window should show five lines: Loop 1 through Loop 5.
  6. Clear the window between runs. Press Ctrl+L while the Immediate Window has focus, or right-click inside it and choose Clear. Clearing makes it far easier to see what the latest run produced, especially when you are printing inside a long loop.
  7. Print real values from your worksheet. Instead of hard-coded numbers, print cell contents, for example Debug.Print Range("A1").Value. You can also print several values on one line by separating them with semicolons, or use a comma to align output in columns. A trailing semicolon keeps the next print on the same line.
  8. Log inside loops and conditions. Put Debug.Print inside a For Each over a range, or inside an If block, to see which branch ran and with what data. This is the main reason to prefer it to MsgBox: the macro keeps running while the output accumulates.
  9. Add a label so lines are identifiable. Print a short prefix with each value, such as Debug.Print "Row " & r & ": " & Cells(r, 1).Value. When fifty lines appear, the prefix tells you immediately which row or iteration produced each one.
  10. Use the window interactively. With the code paused at a breakpoint or stepped through with F8, type expressions directly into the Immediate Window and press Enter to see their current values. You can also assign values this way to test a fix without editing the code.
  11. Remove or comment out the lines before shipping. Debug.Print output is invisible to users of the workbook, but the statements still cost time in large loops. Either delete them or prefix them with an apostrophe so they become comments. A common habit is to keep a module-level constant that turns logging on and off.

Common Mistakes

  • Looking for the output in a cell or a message box. Debug.Print only writes to the Immediate Window. If that window is hidden, the code runs fine and you see nothing.
  • Capitalising the statement incorrectly. VBA is forgiving about case inside the editor, but a typo such as Debug.printt or using Print alone produces a compile error or, worse, prints to a printer dialog in some contexts. Watch the editor's auto-capitalisation: if it does not reformat your line to Debug.Print, something is misspelled.
  • Forgetting that semicolons and commas change the layout. A trailing semicolon suppresses the line break, so the next print continues on the same line. This is useful, but it confuses people who expect one line per value.
  • Printing objects instead of values. Debug.Print Range("A1") prints the default property, which is usually the cell's Value, but it is clearer and safer to write .Value or .Text explicitly.
  • Leaving heavy logging in place. A Debug.Print inside a loop over tens of thousands of cells slows execution noticeably, even though nothing is displayed once the window is closed.
  • Running the wrong procedure. If the cursor is not inside the procedure, F5 may run a different macro or prompt for one. Click inside the correct Sub first.
  • Expecting it to work in Excel for the web or in a protected workbook. The browser version has no VBE, and a password-protected VBA project cannot be opened for editing without the password.

Tips

  • Combine Debug.Print with breakpoints (click the grey margin next to a line, or press F9) and step through with F8 to follow execution line by line.
  • Print a timestamp at the start of a run, for example with the Now function, so you can tell separate runs apart in a long log.
  • Use the Immediate Window as a scratchpad: type ?Range("A1").Value and press Enter for a quick answer without editing your code.
  • If you need output you will study later, write the same information to a text file with the file system commands or to a worksheet column, since the Immediate Window is cleared when the editor closes.
  • Keep log lines consistent in format so you can scan them quickly and compare runs.
  • If you are debugging code that handles personal or sensitive data, remember the window is visible on screen; avoid printing full account numbers or similar values in shared environments.

Troubleshooting

If nothing appears, first check that the Immediate Window is actually visible (Ctrl+G). Next, confirm the procedure you edited is the one that ran: put a breakpoint on the Debug.Print line and see whether execution stops there. If the line is inside an If block that never evaluates true, it will never print, so add a temporary print outside the condition to confirm the loop is reached.

If you get a compile error, read the highlighted line carefully; the usual culprits are a missing ampersand when joining text and numbers, unbalanced quotes, or a misspelled statement. Pressing F8 line by line often shows exactly where things go wrong.

If the output appears jumbled, clear the window (Ctrl+L) and add prefixes to each line. If the macro seems to hang, use Ctrl+Break to interrupt it, which is the standard way to stop a runaway loop in the editor.

If the window itself has disappeared or you cannot dock it, use the View menu to bring it back, and drag its title bar to re-dock it at the bottom of the editor. If macros will not run at all, check Excel's Trust Center macro settings and confirm the workbook was saved in a macro-enabled format.

Frequently Asked Questions

What is the difference between Debug.Print and MsgBox?

MsgBox pauses the macro and demands a click; Debug.Print writes to the Immediate Window and lets the code continue. For loops and long routines, Debug.Print is usually the better choice.

Why does my Debug.Print output run onto one line?

You probably ended the statement with a semicolon or comma. Remove it to get a line break after each print.

Can I use Debug.Print on a Mac?

Yes. The Visual Basic Editor on Excel for Mac includes an Immediate Window, though the keyboard shortcut to open the editor differs from Windows.

Does Debug.Print work in Excel for the web?

No. Excel for the web does not include the Visual Basic Editor, so VBA code cannot be run or debugged there. Use the desktop app.

Will users of my workbook see the Debug.Print output?

Usually not. The Immediate Window only exists inside the editor, so ordinary users of the workbook see nothing. Even so, remove or comment out the lines before distributing the file.