The debug tools in Excel VBA are the Immediate window, the Locals window, breakpoints and the Step Into command, and they let you pause a macro mid-run, inspect what each variable actually holds, and change values on the fly instead of guessing where the error comes from. All of them live behind the Visual Basic Editor (VBE), which you open with Alt+F11 (on Windows) or the equivalent Developer tab shortcut on Mac. Exact button labels vary slightly by Excel version, language pack and platform, so if a ribbon or menu name looks different, look for the closest match. The steps below are written for Excel on Windows because the VBE is most complete there; Mac differences are noted where they matter.

What You'll Need

  • Excel with the Developer tab enabled. If it is missing, go to File > Options > Customize Ribbon and tick Developer. On Mac, open Excel > Preferences > Ribbon & Toolbar and enable it there.
  • A workbook that contains at least one macro, or permission to save a workbook as .xlsm so your VBA code is kept.
  • The Visual Basic Editor available. If it is greyed out or blocked, your organisation may have disabled macros by policy; ask your IT administrator rather than trying to work around it.

Step-by-Step

  1. Enable the Immediate window. Open the VBE with Alt+F11, then press Ctrl+G. If it does not appear, use View > Immediate Window. This pane is your command line: you can type ?Range("A1").Value and press Enter at any time, even when no code is running, to see what a cell holds.
  2. Turn on the Locals window. Choose View > Locals Window. It lists every variable and object in the current scope and updates as you step through code. For a quick look at module-level state without stepping, View > Watch Window lets you add expressions that persist between runs.
  3. Set a breakpoint. Click in the grey margin to the left of a line of code, or click the line and press F9. A dark red dot appears and the line is highlighted. When the macro reaches that line it pauses and the line turns yellow, meaning execution is suspended before that statement runs. Click the dot again or press F9 to remove it. To clear everything, use Debug > Clear All Breakpoints.
  4. Run the macro. Press F5 from inside the procedure, or Run > Run Sub/UserForm. The code runs until it hits your breakpoint and then stops.
  5. Step through the code. Use F8 (Step Into) to execute one line at a time. Step Over (Shift+F8) runs a called procedure in one go and stops on the next line of the current routine, which is handy when you trust a helper function. Step Out (Ctrl+Shift+F8) finishes the current procedure and returns to the caller. Watch the yellow line move down and check the Locals window after each step.
  6. Inspect real values. In the Immediate window type ?myVariable and press Enter to print its value. For objects, type ?Range("A1").Address or ?ActiveSheet.Name. If a variable shows Empty or Nothing, that is usually the bug itself: the code is reading a value it never assigned.
  7. Change values while paused. In the Immediate window, type myVariable = 100 and press Enter, then continue with F8. The rest of the code runs with the new value. This lets you test the "what if the number were bigger" case without editing and restarting.
  8. Resume or stop. Press F5 to run to the next breakpoint or to the end. Press Ctrl+Break (Ctrl+C on Mac) to halt a macro that is stuck or looping forever. If Ctrl+Break does nothing, the loop may be very tight; close Excel with the VBE window still open only as a last resort, and be ready to lose unsaved changes.
  9. Read the error dialog properly. When a run-time error appears, choose Debug rather than End. Debug highlights the failing line and keeps your variables alive so you can inspect them. End wipes the call stack and you lose that context.
  10. Add defensive lines in code. Debug.Print someValue writes to the Immediate window each time that line runs, giving you a running log without breakpoints. Stop acts like a breakpoint written into the code, and Debug.Assert condition pauses only when a condition is false, which is useful for catching a bad value early in a long routine.
  11. Use the Call Stack when deep in procedures. Choose View > Call Stack while paused to see which procedures are waiting on which. This answers the common question "how did I even get here?" when a bug fires inside a function called from three places.

Common Mistakes

The most frequent error is trusting what the code should do instead of what the debugger shows. If the Locals window says a variable is Empty, it is Empty; rewriting the downstream code will not help. Second, many people only ever use F5 and never step, so a wrong line goes unnoticed until output looks odd. Third, breakpoints left in a workbook surprise users later, because the macro pauses for anyone who opens it. Clear breakpoints before you share a file. Fourth, stopping with the End button in the toolbar or the Reset button in the Run menu discards all variable state, so people inspect variables after a reset and find nothing useful. Finally, forgetting that the Immediate window persists between runs leads to stale values being read as current ones; clear it with Ctrl+A then Delete if you want a clean slate.

Tips

Put Option Explicit at the top of every module. It forces you to declare variables, and the compiler then catches typos like lRowCount versus lRowcount before you ever run the code. Use meaningful variable names so the Locals window reads like a summary of your logic. When testing loops over worksheets or rows, temporarily reduce the range to two or three items so you reach the interesting line quickly. If a value looks wrong, break the expression apart: print each part in the Immediate window rather than one long compound expression. And when you find a bug, fix it, then rerun the same path with F8 once to confirm the fix actually changed the value you expected.

Troubleshooting

The macro runs straight past the breakpoint. Check the module you are looking at is the one actually running, and that the dot is in the same procedure as the executing code. If the workbook contains several similar macros, confirm which one the button or shortcut calls. Also check for On Error Resume Next earlier in the procedure: it can silently skip the failing line you expected to stop on, so add a temporary comment to that line and rerun.

Alt+F11 does nothing. The VBE may be disabled by policy, which is common on managed workstations. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and note that only an administrator can change an organisation-wide block. On a Mac, the shortcut differs; use the Developer tab's Visual Basic button instead.

The Immediate window is missing or greyed out. If the whole editor is in Run mode, stop the macro first with Ctrl+Break. A window that refuses input usually means execution is suspended in a place where no scope is available; press the Reset button in the toolbar to return to design time.

Values change unexpectedly between steps. Something in the watched scope is being modified by a called procedure. Before the call, right-click the variable and choose Add Watch, set the watch type to Break When Value Changes, and the editor will stop the moment it is altered.

Excel locks up completely. A tight loop with screen updating on can appear frozen. Press Ctrl+Break; if the machine will not respond, close Excel from Task Manager and accept that recent unsaved edits are lost. In code, DoEvents inside long loops keeps the interface responsive enough to interrupt.

Frequently Asked Questions

What is the difference between F5 and F8 in the VBA editor?

F5 runs the macro normally, stopping only at breakpoints or errors. F8 runs a single line and pauses, which is what you want when hunting for the exact line that produces a wrong result.

Can I debug a macro on Excel for Mac?

Yes. The VBE on Mac includes the Immediate window, Locals window, breakpoints and stepping. Some ribbon or right-click options are worded differently, and Ctrl+Break is often Ctrl+C, but the workflow is the same. Very old Mac Excel versions had no VBA at all, so check your version.

Does deleting the breakpoint change my code?

No. Breakpoints and watches are saved with the workbook as editor settings, not as lines of code, so removing them does not alter the procedure.

Why does my variable say Nothing or Empty?

It was never assigned, or the assignment happened in a different procedure whose variables went out of scope. Verify the variable is declared at the right level and that the assigning line actually ran.

Will Debug.Print lines slow down my finished macro?

Write operations are minor, but leaving many of them in a long loop is untidy and can add overhead. Remove or comment out debugging lines once the code works, or guard them behind a flag.

What is the Locals window showing that the Immediate window does not?

Locals lists every variable and object in scope automatically, with nested properties you can expand. The Immediate window shows only what you type, which is faster for one-off checks but does not give the full picture.