Wauvel

Excel like a finance pro.

← All VBA snippets

Find out why a macro isn't doing what you expect

Debug.Print, breakpoints and F8 — how to watch a macro run instead of guessing why it didn't.

1What it does

When a macro does the wrong thing, most people rewrite it. You almost never need to: you need to see what it saw. `Debug.Print` writes to the Immediate window as the macro runs, so you get a log of what each row actually held. F8 steps one line at a time with the sheet visible beside you, and clicking the grey margin sets a breakpoint that runs at full speed until it reaches that line. Nine times in ten the answer is that a value you assumed was a number is text, and the comparison you wrote has been doing something quietly different all along.

2The code

Sub FlagLargeItems()
    Dim lastRow As Long, r As Long, flagged As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row

    ' Ctrl+G in the editor opens the Immediate window this writes to.
    Debug.Print "--- run " & Now & " · rows 2 to " & lastRow

    For r = 2 To lastRow
        ' Click the grey margin beside this line for a breakpoint,
        ' or uncomment Stop to break here every time.
        ' Stop

        ' TypeName is the one that solves it: "Double" vs "String".
        Debug.Print r, Cells(r, 2).Value, TypeName(Cells(r, 2).Value)

        If Cells(r, 2).Value > 10000 Then
            Cells(r, 3).Value = "Review"
            flagged = flagged + 1
        End If
    Next r

    Debug.Print "flagged " & flagged & " of " & (lastRow - 1)
End Sub

Paste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).

New to macros? Set up in 5 minutes
  1. 1

    Don't see the Developer tab in the ribbon?

    You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.
    • Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
    • Mac: Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
  2. 2

    Paste in the code

    Press Alt + F11 to open the Visual Basic editor, then Insert → Module and paste the snippet's code into the blank window. Close it with Alt + Q.
  3. 3

    Run it

    Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in: =GrossMargin(B2, B3).)
  4. 4

    Keep the macro — save as .xlsm

    File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save.
  5. 5

    Macros blocked?

    Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.

Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.

3When you use it

  • A loop that flags too many rows, or none at all.
  • Finding the one row in four thousand that breaks the run.
  • Checking what a cell actually contains before blaming the formula.

4See it in action

A simulation — press Run to perform what the macro does to a sample workbook.

Immediate window (Ctrl + G)

Press Step to walk the loop one row at a time.

5Pitfalls

The Immediate window isn't visible, so Debug.Print appears to do nothing.

Fix: `Ctrl + G` in the VBA editor. It's a panel, not a dialog — it may be docked at the bottom already.

Debug.Print inside a loop over 100,000 rows slows the macro to a crawl, and the window only keeps the last ~200 lines anyway.

Fix: Print only when something interesting happens: `If r Mod 500 = 0 Then Debug.Print r`.

`> 10000` silently compares text, because the column arrived from an export as text. "9" > 10000 is TRUE as a string comparison.

Fix: That's why TypeName is in the log above. If it says String, convert the column first.

Stepping with F8 through a long loop takes forever.

Fix: Set a breakpoint INSIDE the `If`, so it runs at full speed and stops only on the rows you care about.

6No-code alternatives

  • MsgBox in the loop Works, and stops the macro dead on every row waiting for a click. Fine once, miserable over a hundred rows.
  • The Watch window For keeping an eye on one variable across the whole run instead of printing a log.

Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.