To turn off an error handler in the current Excel VBA procedure, use On Error GoTo 0. It disables that procedure’s active handler; it does not prevent errors from occurring or change error handling in other procedures.
What turning off error handling means
VBA error handling controls what happens when a statement raises a runtime error. A procedure can send execution to a labeled handler with On Error GoTo HandlerName, or tell VBA to continue after an error with On Error Resume Next. If either form is active, On Error GoTo 0 switches off that procedure’s enabled error handling.
After the switch is off, an error that is not handled elsewhere follows VBA’s normal behavior: execution may stop and the Visual Basic Editor may display an error message. The instruction does not erase the cause of an error, repair a workbook, or make later statements safe. It simply changes how the current procedure responds to a subsequent error.
Error handling is local to a procedure. Turning it off in one Sub or Function does not turn off a handler in the procedure that called it, or in another independent procedure. This scope matters when a macro is divided into several routines.
Use On Error GoTo 0 to disable a handler
Place the statement at the point where you want normal error behavior to resume. It can turn off either a label-based handler or Resume Next mode. For example, if a macro temporarily ignores an error while checking whether a worksheet exists, turn that behavior off immediately after the risky statement:
Sub CheckForTempSheet()
Dim ws As Worksheet
' Save a copy of the workbook before running a macro.
On Error Resume Next
Set ws = ThisWorkbook.Worksheets("Temp")
On Error GoTo 0
If ws Is Nothing Then
Debug.Print "The Temp sheet was not found."
Else
Debug.Print "The Temp sheet exists."
End If
End Sub
The worksheet lookup raises an error if the named sheet is absent. While Resume Next is active, VBA proceeds to the next statement instead of stopping. On Error GoTo 0 then ends that temporary behavior. The test for Nothing lets the macro make an explicit decision rather than silently treating every lookup failure as success.
Save a copy of the workbook before running a macro: VBA actions generally cannot be undone with Ctrl+Z. On Windows, Alt+F11 opens the Visual Basic Editor; on Mac, the editor shortcut can depend on the keyboard and system settings. Excel for the web cannot run VBA macros, so use desktop Excel.
Turn off Resume Next as soon as possible
On Error Resume Next is easy to overuse because it can make a macro appear to run past problems. It applies to subsequent statements in that procedure until another error-handling instruction changes the mode or the procedure ends. If it remains active, a failed operation may be followed by code that assumes the operation succeeded.
Keep the ignored operation narrow: enable Resume Next, execute only the statement whose error you expect, inspect the outcome, and immediately use On Error GoTo 0. Check an object variable for Nothing, or examine the Err object when you need the error number or description. Do not leave Resume Next active across a long block of unrelated workbook operations.
For instance, ignoring an error during one optional worksheet lookup may be reasonable if the macro then checks whether the sheet reference was set. Ignoring errors while reading, changing, saving, and closing a workbook is much harder to reason about: a failed step can leave later code working with incomplete or incorrect data.
How VBA handlers work
A label-based handler gives a procedure a defined route for responding to errors. The statement On Error GoTo HandleError enables that route. If a runtime error occurs while the handler is enabled, VBA transfers control to the named label. A handler should report or deal with the problem and then exit or resume execution deliberately.
There is an important distinction between an enabled handler and an active handler. A handler becomes active when VBA transfers control to it. It cannot handle another error while it is already active. If an error occurs inside the handler before the first one has been dealt with, VBA may pass the new error back to the calling procedure. This is one reason to keep handler code simple and avoid risky operations inside it.
On Error GoTo 0 disables the enabled handler in the current procedure. It does not jump to a label, clear a worksheet error, or automatically resume at the statement after the one that failed. Those are separate control-flow decisions. Choose the destination after handling an error with a suitable Resume, Exit Sub, or an explicit jump to cleanup code.
Use cleanup code for predictable exits
Macros that open files, alter application settings, or change workbook state often need cleanup whether they finish normally or encounter an error. A common structure routes errors to a handler, then directs both the normal path and the handled-error path to a cleanup section. At cleanup, the handler can be disabled before the procedure finishes:
Sub ReadConfigValue()
Dim configValue As Variant
' Save a copy of the workbook before running a macro.
On Error GoTo HandleError
configValue = ThisWorkbook.Worksheets("Config").Range("B2").Value
GoTo CleanUp
HandleError:
Debug.Print "Could not read the configuration: " & Err.Description
Resume CleanUp
CleanUp:
On Error GoTo 0
Debug.Print "Procedure finished."
End Sub
If the worksheet access succeeds, the macro goes directly to cleanup. If it fails, VBA enters the handler, prints the error description, and resumes at cleanup. The cleanup label disables the handler and lets the procedure end normally. A real macro may need additional cleanup actions, such as closing a file it opened; those actions should be written so they do not hide the original failure.
Do not assume that putting On Error GoTo 0 at the end automatically makes a handler robust. If the macro exits early, skips the line, or enters the handler and never reaches cleanup, the intended structure may not be followed. Use clear exits and test both the success path and the error path.
Common mistakes and misconceptions
- Confusing Err.Clear with disabling a handler:
Err.Clearclears the stored error information in theErrobject. It does not switch offOn Error Resume Nextor a label-based handler. UseOn Error GoTo 0to disable the enabled handler. - Expecting GoTo 0 to undo an error: It does not reverse a change already made to a cell, restore a file, or retry a failed statement. It affects error handling from that point forward.
- Assuming it affects the whole workbook: The instruction applies to the current procedure, not every module or macro in the workbook. Each procedure’s error handling should be understood on its own.
- Leaving Resume Next on after an expected error: This may cause a later, unrelated failure to be skipped too. Put the disabling statement directly after the narrow operation that may fail.
- Using On Error GoTo 0 as a way to suppress error messages: It does the opposite of broad suppression: an error that occurs afterward is no longer caught by that procedure’s enabled handler.
Errors are different from Excel alerts
VBA error handling is not the same as Excel’s confirmation prompts or application alerts. For example, Application.DisplayAlerts controls certain Excel prompts; it is not a substitute for On Error GoTo 0 and does not provide a general way to ignore VBA runtime errors. Similarly, hiding a message box or turning off a particular prompt does not make an invalid VBA operation succeed.
Before suppressing an error, identify which kind of problem you are seeing. A runtime error in the Visual Basic Editor, a warning dialog from Excel, a compile error, and a message displayed by the macro are different things. On Error handles runtime errors; it does not repair syntax errors that prevent the code from compiling.
How to verify the change
After adding On Error GoTo 0, read the procedure from top to bottom and mark where each error-handling instruction takes effect. Then check what happens if the expected operation succeeds and what should happen if it fails. A handler that is disabled too early will not catch later errors; one disabled too late may hide failures you wanted VBA to expose.
Use the Visual Basic Editor’s debugging tools to inspect execution. On Windows, F8 steps through code one statement at a time; on Mac, the equivalent key can vary with the keyboard and macOS settings. Check the Err.Number and Err.Description values near a failure when appropriate, and remember that clearing those values is distinct from changing the active error-handling mode.
Test on a copy of the workbook, especially if the macro edits data or changes application settings. A useful test includes the ordinary success case and the specific failure the handler is meant to address, such as a missing optional sheet. Avoid deliberately triggering an unhandled error in a workbook that contains unsaved changes.
Frequently Asked Questions
What is the VBA command to turn off error handling?
Use On Error GoTo 0 in the procedure where you want the enabled handler disabled. It applies from that point onward in that procedure.
Does On Error GoTo 0 clear the Err object?
Do not rely on it to clear stored error details. Err.Clear clears the Err object’s information, while On Error GoTo 0 disables the current procedure’s enabled error handler. They serve different purposes.
How do I stop On Error Resume Next?
Put On Error GoTo 0 after the statement whose error you intended to ignore. Then check the result of that statement before continuing with dependent work.
Does On Error GoTo 0 turn off error handling in every subroutine?
No. Error handling is set within each procedure. Calling a procedure that uses its own handler does not make On Error GoTo 0 in the caller change that procedure’s settings.
Will turning off error handling stop Excel from showing errors?
No. It disables the current procedure’s handler. If a later runtime error is not handled by another applicable handler, VBA’s normal error behavior can stop execution and show an error in the editor.
Related
- Excel Turn Off Page Break Lines on Screen
- Excel Page Break Dotted Lines: How to Turn Them Off
- Excel: Turn Off Page Break Preview and Restore Normal View
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Copy Paste Special: Values, Formats, and MoreIn Excel VBA, copy a range and call PasteSpecial on the destination, specifying what to paste. For example,…
- VBA Copy and Paste Values Only in ExcelTo copy a range in Excel VBA and paste only its values, use Copy followed by PasteSpecial Paste:=xlPasteValues.…
- Do Excel Macros Work in Google Sheets?No. Excel macros written in VBA do not run in Google Sheets, and converting an Excel workbook does…
- Excel VBA Copy and Paste Formatting: Code and TipsTo copy only formatting with Excel VBA, copy the source range and use PasteSpecial with xlPasteFormats: sourceRange.Copy, then…
- Excel Macro Filter: How to Automate Data FilteringTo filter a worksheet with a macro, use VBA’s AutoFilter method on a range that includes its header…
- Excel VBA String to Number: Safe Conversion MethodsIn Excel VBA, convert text that represents a number with a conversion function such as CDbl for decimals…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.