When writing macros in Excel VBA, managing errors is essential to keep your code reliable. By default, VBA uses the behavior of On Error GoTo 0, which means if an error occurs, the macro stops immediately and shows an error message. This helps identify problems quickly during development.
In this article, we’ll explore how On Error GoTo 0 works in Excel VBA, why it’s important, and how it resets error handling to the default state. You’ll learn when to use it effectively and how it differs from other error handling methods to make your macros more stable and easy to debug.
Steps to use On Error GoTo 0 to restore normal error handling after skipping one known error in Excel VBA:
➤ Open the VBA Editor (press Alt + F11).
➤ Insert a new module and paste the following code:
Sub SkipThenRestore_WithGoTo0()
On Error Resume Next
Worksheets("Temp").Delete ' Ignore if sheet is missing
On Error GoTo 0 ' Restore normal handling
Dim x As Long, total As Double
total = Application.WorksheetFunction.Sum(Range("B2:B8"))
x = total / 0 ' This triggers run‑time error 11
MsgBox "Average salary is " & x
End Sub
➤ Run the macro in Excel (press F5).
➤ The missing‑sheet error is skipped. VBA then halts on the divide‑by‑zero line and shows Run‑time error 11: Division by zero.
Use On Error GoTo 0 to Restore Normal Error Handling
Sometimes you want to ignore a specific, predictable error in your VBA code, like trying to delete a worksheet that might not exist, without hiding any other unexpected errors. You can do this by placing On Error Resume Next just before the risky line to skip that error, then immediately turning normal error handling back on with On Error GoTo 0. This ensures only the known error is ignored, while other errors will still stop your macro and show messages.
For this demonstration, we’ll use a workbook with a worksheet named Data. There is no worksheet called Temp, so attempting to delete it will cause an error. After that, the macro performs a division by zero, which should trigger a run-time error if normal error handling is active.
Using this technique, the macro quietly skips the missing worksheet error but correctly stops and shows an error message when the division by zero occurs. This confirms that the error handling is restored immediately after the expected error is skipped.
Steps:
➤ Press Alt + F11 to open the VBA Editor, then choose Insert >> Module.
➤ Paste the following code without restoring error handling:
Sub SkipButHide_NoHandler()
On Error Resume Next
Worksheets("Temp").Delete ' Sheet may not exist
' Error skipping is still ON here
Dim x As Long, total As Double
total = Application.WorksheetFunction.Sum(Range("B2:B8"))
x = total / 0 ' Divide by zero is silently ignored
MsgBox "Macro finished."
End Sub
➤ Return to Excel and press F5 to run the macro.
You will notice the macro finishes without any error pop-ups even though a division by zero occurred. This happens because error skipping remained active after deleting the missing sheet, so the second error is hidden.
➤ Now update the macro to restore normal error handling:
Sub SkipThenRestore_WithGoTo0()
On Error Resume Next
Worksheets("Temp").Delete ' Ignore if missing
On Error GoTo 0 ' Restore normal handling
Dim x As Long, total As Double
total = Application.WorksheetFunction.Sum(Range("B2:B8"))
x = total / 0 ' Now triggers run-time error 11
MsgBox "Average salary is " & x
End Sub
➧ On Error GoTo 0 immediately restores normal error handling, so any errors that occur after that point are displayed normally.
➧ In the first macro, skipping stayed active which masked the division by zero error and let the macro finish silently.
➧ In the improved macro, only the expected error is ignored while later errors like division by zero still cause a proper error message making debugging easier and your code more reliable.
➤ Run this updated macro again by pressing F5 .
This time, VBA will stop at the division by zero line and show the error message: Run-time error ‘11’, Division by zero.
Test for Missing Worksheet Before Proceeding Using On Error GoTo 0
When your VBA code accesses worksheets that might not exist, trying to refer to a missing sheet causes an error that stops your macro. Using On Error Resume Next before the check lets your code try accessing the sheet without halting. Immediately after, placing On Error GoTo 0 restores normal error handling so later errors won’t be ignored. This lets you safely test if a sheet exists and then decide what to do next.
We’ll use the Data sheet to test for a sheet named “Report”. The macro tries to set a worksheet variable to “Report”. If the sheet doesn’t exist, an error would occur, but with error handling it skips the error, allowing the code to detect the missing sheet and notify the user.
Steps:
➤ Press Alt + F11 to open the VBA Editor and insert a new module.
➤ Paste and run this code without error handling first:
Sub CheckSheet_NoHandler()
Dim ws As Worksheet
Set ws = Worksheets("Report") ' Error if "Report" sheet missing
MsgBox "Sheet found."
End Sub
➤ If the “Report” sheet does not exist, the macro throws an error and stops.
➤ Now update the macro with proper error handling:
Sub CheckSheet_WithErrorHandling()
Dim ws As Worksheet
On Error Resume Next ' Skip error if sheet not found
Set ws = Worksheets("Report")
On Error GoTo 0 ' Restore normal error handling
If ws Is Nothing Then
MsgBox "Sheet 'Report' not found."
Else
MsgBox "Sheet found."
End If
End Sub
➧ On Error GoTo 0 resets error handling so further errors are not missed.
➧ This method safely tests for the existence of a worksheet without stopping your macro unexpectedly.
➧ It provides a controlled way to handle missing objects while preserving debugging for other issues.
➤ Run this version. It will either show “Sheet found” if present or “Sheet ‘Report’ not found” without stopping due to an error.
Utilize On Error GoTo 0 After Ignoring a File Not Found Error in Excel VBA
When working with external files in VBA, you may try to open or read a file that doesn’t exist. Without error handling, this causes a run-time error and stops your macro. By using On Error Resume Next before the risky operation and restoring default handling right after with On Error GoTo 0, you can handle just the expected error without hiding others.
In this example, the macro attempts to open a workbook file named Sales2023.xlsx located in the same folder. If the file doesn’t exist, VBA would normally stop. But with error handling, we can skip this step safely and display a message instead.
Steps:
➤ Press Alt + F11 to open the VBA Editor, go to Insert >> Module, and paste this code without error handling:
Sub OpenFile_NoHandler()
Workbooks.Open Filename:=ThisWorkbook.Path & "\Sales2023.xlsx"
MsgBox "File opened successfully."
End Sub
➤ If Sales2023.xlsx is not found in the current folder, this macro throws a run-time error and halts.
➤ Now update the macro with error handling using On Error Resume Next and On Error GoTo 0:
Sub OpenFile_WithHandling()
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Sales2023.xlsx")
On Error GoTo 0
If wb Is Nothing Then
MsgBox "File not found. Please make sure Sales2023.xlsx is in the same folder."
Else
MsgBox "File opened successfully."
End If
End Sub
➧ On Error GoTo 0 ensures that any later errors are not hidden.
➧ The If wb Is Nothing check confirms whether the file opened so you can respond accordingly.
➧ This method keeps your file-handling macros smooth and user-friendly, even when files are missing.
➤ Run the updated macro. If the file exists, it opens and shows a success message. If not, it displays a clear message without any error pop-up.
Frequently Asked Questions
What does On Error GoTo 0 actually do?
It disables any active error handler in the current procedure, returning VBA to its default behavior, stopping on errors, and showing a message.
Do I need to use On Error GoTo 0 if I didn’t set any handler?
No, VBA defaults to GoTo 0 behavior. But explicitly placing it after On Error Resume Next makes your intent clear and avoids hidden errors.
How is On Error GoTo 0 different from On Error GoTo -1?
On Error GoTo 0 disables the error handler; On Error GoTo -1 also clears the current error state. Use -1 to reset the error state fully.
Why use On Error GoTo 0 after a temporary error skip?
After using On Error Resume Next to ignore a known issue, On Error GoTo 0 restores default error handling so any later unexpected errors are reported.
Wrapping Up
Using On Error GoTo 0 is a simple but essential part of writing safe and professional VBA code. While On Error Resume Next lets you skip known issues, On Error GoTo 0 ensures you don’t accidentally hide important errors that follow. By resetting VBA’s default error handling, it helps you isolate expected exceptions without compromising the integrity of your macro.