To debug Excel VBA, use the three tools built into the Visual Basic Editor (VBE): breakpoints (F9) to pause code on a line, Step Into (F8) to run one statement at a time, and the Immediate Window (Ctrl+G) to print or change values while paused. Turn on Tools > Options > General > Break on All Errors early, because the default setting hides errors inside error-handling blocks and makes bugs look like they appear from nowhere.
Debugging is not about guessing. VBA's editor lets you freeze a running macro mid-procedure, inspect every variable and object property, and resume line by line. Once you can watch a value change on the exact line where it breaks, most bugs resolve in minutes. Labels in the VBE can differ slightly by Excel version, so if a menu name looks different, the command is still there under a similar heading.
What You'll Need
- Excel on Windows (the full VBE and its debugging toolbar are a desktop feature; Excel for the web cannot open or debug VBA at all).
- A macro-enabled workbook saved as .xlsm, .xlsb or .xlam, since .xlsx strips VBA.
- Macro security set so the project can run — either the file is in a Trusted Location or macros are enabled for the session. If macros are blocked, code never executes and nothing can be debugged.
- Access to the VBE: press Alt+F11 (on Mac Excel, the VBE is also available, but the menu layout and some dialogs differ).
- Ideally, a backup copy of the workbook before you start editing code.
Step-by-Step
- Reproduce the failure deliberately. Write down the exact sheet, cell selection and input that triggers the error. A bug you cannot reproduce is a bug you cannot confirm as fixed. Run the macro from the same starting state each time.
- Open the editor and find the procedure. Press Alt+F11, then Ctrl+R to show the Project Explorer. Expand VBAProject, then Modules, and double-click the module containing your macro. If the error dialog is on screen, click Debug instead of End — this jumps straight to the highlighted offending line.
- Set your error behaviour. In the VBE choose Tools > Options > General tab and select Break on All Errors. Now any runtime error stops the code, even inside an
On Error Resume Nextblock. Switch back to Break on Unhandled Errors when you finish debugging. - Add a breakpoint. Click in the grey margin beside a line, or place the cursor on the line and press F9. A dark red dot and highlighted line mean the code will stop there before executing that statement. Use F9 again to clear it. Breakpoints are not saved with the workbook when you close it.
- Run the macro to hit the breakpoint. Press F5 in the editor to run from the start, or trigger the macro from Excel. When execution stops, the line is highlighted in yellow and the word [break] appears in the title bar. Your code is still alive — nothing after that line has run.
- Step through the code. Press F8 (Step Into) to execute one statement and move to the next. Use Shift+F8 (Step Over) to run a whole called procedure without entering it, and Ctrl+Shift+F8 (Step Out) to finish the current procedure and return to the caller. Step Into is your default; Step Over is for procedures you already trust.
- Inspect variables several ways. Hover the mouse over any variable to see its current value. Click View > Locals Window to see every variable in scope with its type and value. Choose View > Watch Window, then right-click a variable in code and pick Add Watch to monitor it, or set a watch expression that breaks when a value changes or becomes True.
- Query the Immediate Window. Press Ctrl+G to open it. Type
?myValueand press Enter to print a value; type?Range("A1").Valueto read a cell; type?UBound(myArray)to check array bounds. You can also assign while paused, e.g.myCount = 0, then continue with F5 to test a different path without editing code. - Check the call stack. Choose View > Call Stack while paused inside nested procedures to see the chain of calls and which line called which. This is the fastest way to find out why a helper routine received the wrong argument.
- Verify object assumptions. Most VBA errors are not logic errors but wrong assumptions: the active sheet is not the sheet you think, a range is empty, a name does not exist. In the Immediate Window, print
?ActiveSheet.Name,?Selection.Addressor?IsEmpty(Range("A1"))before blaming the code. - Test small pieces in isolation. Select just the suspect lines in the editor and press F5 — VBA runs only the selected lines, provided they form a valid, self-contained block. This shortcuts the need to restart a long macro from the top.
- Trace execution order. If a macro does something unexpected and you cannot see where, right-click in the code window and choose Toggle > Breakpoint line by line down the routine, or insert temporary
Debug.Printstatements with a marker such asDebug.Print "Step 3:", myValue. Read the output in the Immediate Window, then delete the lines. - Handle errors properly, then retest. Replace blanket
On Error Resume Nextwith targeted handling. Structure it asOn Error GoTo Handler, run the risky statement, thenOn Error GoTo 0immediately after so later errors are not swallowed. In the handler, logErr.NumberandErr.DescriptionwithDebug.Print, then decide whether to exit or re-raise. - Remove debugging debris. Delete breakpoints (Debug > Clear All Breakpoints or Ctrl+Shift+F9), remove temporary
Debug.Printlines andStopstatements, reset error handling to your normal setting, and run the whole macro once from a clean state to confirm it works end to end.
Common Mistakes
- Leaving
On Error Resume Nextswitched on. It silently skips the failing line, so variables stay blank and the visible error appears somewhere else entirely. This is the single most common reason a bug seems impossible to find. - Setting a breakpoint on a declaration line (
Dim x As Long) or anElse/End Ifline. Declarations are not executable, so the code will not stop there; breakpoints must sit on statements that actually run. - Debugging a copy of the workbook. If two files are open, you may edit one project and run another. Check the title bar in the VBE for the workbook name before starting.
- Forgetting that breakpoints and watches vanish when you close the workbook, and that the Immediate Window contents clear when you press the Reset button (the blue square on the toolbar).
- Ignoring the difference between
ValueandText, and between a number stored as text and a real number. A cell showing100can be a string, and comparisons silently fail. - Editing code while paused mid-procedure. Changing structure such as adding or removing lines can force a reset and lose your state; stop, edit, then rerun.
- Using
ActiveSheetandSelectionwhen the macro should target an explicit sheet and range. Debugging becomes hard because behaviour depends on what the user clicked.
Tips
- Beyond F8, Shift+F8 and Ctrl+Shift+F8, learn Run to Cursor (Ctrl+F8): place the cursor further down and jump execution straight to that line without setting a breakpoint.
- Use
Debug.Assertfor assumptions:Debug.Assert Not IsEmpty(Range("A1"))halts the code the moment the condition is false, without leaving output behind. - Add
Option Explicitat the top of every module. It forces you to declare variables, so a typo likemyCuontbecomes a compile error instead of a silently empty new variable. - Compile first: Debug > Compile VBAProject catches syntax and declaration errors before you ever run the macro.
- Wrap reusable test helpers in a dedicated module — a routine that prints every variable's value in a labelled block keeps your real code clean.
- Keep a backup before large edits. Version the workbook with a date in the filename, since Excel offers no undo inside the VBE.
- If a macro suddenly stops working after an Office update, test a trivial macro first. That isolates whether the problem is your code or the environment.
Troubleshooting
- Code never stops at the breakpoint. Confirm the macro actually runs (add a
MsgBoxat the top), that the breakpoint is on executable code, and that you are in the right workbook project. If the procedure is not being called at all, check the button, shortcut or event that is meant to trigger it. - The macro finishes instantly with no error but no result. An error handler is probably swallowing the failure. Temporarily comment out the
On Error Resume Nextline, or set Break on All Errors, and run again. - Running in the editor does nothing. Press the Reset button first. A previous run that is still paused blocks new runs and leaves variables frozen in their old state.
- Macros are disabled and nothing executes. Check the yellow security bar, the file extension, and whether the folder is trusted. Code cannot run while macros are blocked.
- The VBE does not open or the debugging items are greyed out. The project may be password-protected, or the workbook is open in another session. Close extra Excel instances, and if a password was set by someone else, you cannot bypass it — ask the author for the password or an unprotected copy.
- Excel hangs completely. If the code is stuck in an endless loop (
Do ... Loopwith no exit), press Esc or Ctrl+Break to interrupt, or end the Excel process and reopen the workbook. Save your work before the run next time. - The error appears only on another machine. Missing references (Tools > References) or different locale settings for dates and decimals are the usual culprits. Compare the reference list on both machines.
Frequently Asked Questions
What is the shortcut to stop VBA code while it is running?
Press Ctrl+Break, or Esc, while the code is executing. If the loop is blocking input, end the Excel process from Task Manager and reopen the file.
Does a breakpoint stay when I close the workbook?
No. Breakpoints, watches, call-stack history and the Immediate Window contents are cleared when the project is reset or the workbook is closed. Only changes to the code itself are saved.
Can I debug VBA in Excel for the web?
No. Excel for the web does not include the Visual Basic Editor. You need the desktop application to write, break into or step through macros.
Why does On Error Resume Next make debugging harder?
It tells VBA to skip any failing line and carry on, so execution continues with variables left at their previous or default values. The error surfaces later, far from its cause. Turn it off temporarily or set Break on All Errors to find the real failing line.
What does the yellow line mean when my code stops?
It is the statement about to run. Nothing on that line has executed yet, so inspecting values shows the state before the line runs. Press F8 to execute it and watch what changes.
Should I use Debug.Print or MsgBox for tracing?
Use Debug.Print for tracing — it writes to the Immediate Window without interrupting execution, so you can watch a loop run. Reserve MsgBox for points where you genuinely need the code to pause and show one value, since it stops everything until you click OK.
How do I find which line is causing a runtime error?
When the error dialog appears, click Debug. The editor opens at the failing line in yellow if Break on All Errors is enabled; otherwise it highlights the line inside the error handler. From there, use the Immediate Window to print the variables involved.