If On Error GoTo seems to do nothing in Excel VBA, the cause is almost always one of four things: the error is a compile error (which the handler can never catch), the handler label is missing or misspelled, the routine has already exited via Exit Sub before the error, or the macro is running in break mode with error trapping set to "Break on All Errors". Fix the handler structure first, then check the VBA error-trapping setting in Tools > Options > General. The steps below cover the correct pattern, the traps that silently break it, and how to confirm each one.

What You'll Need

  • Excel with the Developer tab visible (File > Options > Customize Ribbon > tick Developer), or press Alt+F11 to open the VBA editor directly.
  • The macro that is misbehaving, and a way to reproduce the error reliably (a known bad value, a missing file, a closed workbook).
  • A backup copy of the workbook before you edit code or change editor settings.
  • Basic familiarity with the VBA editor layout: Project Explorer, code window, and the Immediate Window (Ctrl+G).

Menu labels in Excel and the VBA editor can differ slightly by version, platform and region, so if a label does not match exactly, look for the closest equivalent in the same menu.

Step-by-Step

  1. Confirm the error is a runtime error, not a compile error. Press Debug > Compile VBAProject in the VBA editor. If the compiler flags a line (a missing variable declaration, a bad argument count, an undefined procedure), fix that first. On Error GoTo only catches errors that happen while code runs, never errors the compiler finds before running. A red-highlighted compile error will always stop the macro no matter how good your handler is.

  2. Check the editor's error trapping setting. In the VBA editor go to Tools > Options > General tab and look at the Error Trapping group. If "Break on All Errors" is selected, VBA ignores your handlers and breaks on every error. Choose "Break on Unhandled Errors" (the usual setting) so your On Error GoTo label gets control. Click OK and re-run.

  3. Verify the label exists and matches exactly. The label must sit on its own line, end with a colon, and use identical spelling and capitalisation is not required but exact characters are: On Error GoTo Handler jumps to a line reading Handler:. A label with a typo, a missing colon, or an extra character is treated as a jump to a line that does not exist, which itself raises an error. Place the handler label after an Exit Sub line, as in the pattern below, so normal runs do not fall into it.

  4. Use the canonical structure. The reliable layout is: enable the handler early in the procedure, put Exit Sub (or Exit Function) before the handler, then the label, the error message, and a Resume or Resume Next or clean exit. Example:

    Sub Demo()
        On Error GoTo Handler
        Dim v As Variant
        v = 1 / 0          ' deliberate runtime error
        Exit Sub
    Handler:
        MsgBox "Error " & Err.Number & ": " & Err.Description
        Resume Next
    End Sub

    Without that Exit Sub, a successful run keeps going straight into the handler text, which can look like the handler "always fires" or behaves oddly.

  5. Check scope: which procedure is actually failing? On Error applies only to the procedure it is declared in. If your failing code lives in a called function, the handler in the caller does not see that function's error unless the called routine passes it up (for example with Err.Raise or by having no handler of its own). Put a handler in the procedure where the risky statement sits, or remove the inner handler and let the error bubble to the outer one.

  6. Look for a second On Error statement. A later On Error GoTo 0 or another On Error GoTo replaces the previous handler. On Error GoTo 0 turns trapping off completely, so all later errors go straight to the default break behaviour. Search the whole procedure (and any included modules) for any second On Error line and decide which one you actually want active at each point.

  7. Step through with F8 to see the real flow. Click inside the procedure and press F8 repeatedly. Watch which line is highlighted when the error occurs. If you never reach the handler label, the problem is in step 1, 2 or 4. If you reach it but the message is blank, Err was cleared (often by a Resume before you read it) — capture Err.Number and Err.Description into variables at the top of the handler before any other statement.

  8. Handle the special non-trappable errors. A few conditions bypass On Error GoTo: running out of stack through unbounded recursion, pressing Ctrl+Break, and certain fatal memory errors. If your macro dies with "Out of stack space", the fix is code structure (add a depth counter or convert the recursion to a loop), not error handling. Also, an infinite loop never triggers a handler because no error occurs — if the macro appears hung, the handler cannot help.

  9. Add diagnostics to prove the handler ran. Put Debug.Print "Entered handler " & Err.Number as the first line of the handler and open the Immediate Window with Ctrl+G. If nothing prints when the error triggers, the handler genuinely never ran and you should revisit steps 1–6 rather than the message text.

Common Mistakes

  • Placing the handler above the code that errors. VBA executes top to bottom; a label defined before the failing line still works as a jump target, but if the handler block sits in normal flow the routine runs into it. Always keep the handler after Exit Sub.
  • Expecting compile errors to be caught. Undeclared variables under Option Explicit, wrong argument counts and syntax errors are caught at compile time and cannot be trapped.
  • Forgetting that Err is global and gets cleared. A successful Resume, On Error GoTo -1, or reaching an Exit Sub all reset Err. Read the values you need immediately in the handler.
  • Assuming handlers stack. They do not. The newest On Error replaces the old one within that procedure.
  • Using On Error Resume Next as a blanket fix. It hides errors instead of handling them, and it suppresses every later error in the routine until you reset it.
  • Omitting Exit Sub from the handler path. Falling through from normal code into the handler produces confusing behaviour that people misdiagnose as the handler firing incorrectly.

Tips

  • Use Err.Source and Erl (with line numbers) alongside Err.Number and Err.Description for better diagnostics.
  • In handlers, log or message the error, then end with a clean exit such as Resume ExitHere where ExitHere: sits just before End Sub. This avoids resuming into the same failing line and looping forever.
  • Keep one handler per procedure and set it as the first executable line, so nothing can fail before protection is in place.
  • When a macro throws a 1004 "Application-defined or object-defined error", that is often an ordinary runtime problem (a bad range reference, a closed workbook) rather than a missing handler — the label will catch it, but the fix is correcting the reference.
  • Store the workbook as .xlsm or .xlsb; a .xlsx file cannot retain macros and will silently drop your handler code on save.

Troubleshooting

If the handler still does not seem to fire, work through this order. First, run Debug > Compile VBAProject again after every edit — a small syntax slip invalidates the whole module and reverts behaviour. Second, recheck Tools > Options > General > Error Trapping and set it to "Break on Unhandled Errors". Third, press Ctrl+F and search the module for On Error to make sure you have not left a On Error GoTo 0 or a duplicate statement somewhere above the failing line. Fourth, confirm the error happens in the same procedure you are editing — a Worksheet_Change or Workbook_Open event handler in another module may be the one that actually faults. Fifth, close and reopen Excel to clear a stuck break state, then test with a deliberately obvious error such as dividing by zero to see whether the handler responds at all.

If the macro now catches the error but shows a blank or unhelpful message, the cause is almost always that Err was reset before you read it, or that the handler was entered through Resume Next rather than an actual error. Capture the values first, then display them. If the macro loops indefinitely after a Resume, the handler is resuming to the same failing line; add a clean-exit label and resume there instead.

Frequently Asked Questions

Why does On Error GoTo do nothing at all?

Usually because the error is a compile error, the editor is set to "Break on All Errors", or a later On Error GoTo 0 turned trapping off. Check all three.

Can On Error GoTo catch a missing variable in Option Explicit mode?

No. Compile errors stop the project before any line runs, so no runtime handler can intercept them. Fix the declaration.

What is the difference between Resume and Resume Next?

Resume retries the statement that failed, running any code before it again. Resume Next continues at the line after the failing statement. Using Resume in a handler for something like a missing file can cause an endless loop.

Does On Error GoTo work in event procedures?

Yes, it works inside any procedure, including event handlers, but only in the same procedure where the failing line sits. The handler must be declared in that event routine.

Why does my error number become zero in the handler?

Err is cleared by Resume, Exit Sub, and On Error GoTo -1. Read Err.Number and Err.Description into variables on the first lines of the handler.

Can I have nested error handlers in one Sub?

No. Each On Error statement replaces the current handler within that procedure. To get separate handling, split the code into its own procedure and trap errors there.