EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Error Handling Functions VBA On Error GoTo 0
 

VBA On Error GoTo 0

Madhuri Thakur
Article byMadhuri Thakur

VBA On Error Goto 0

Excel VBA On Error Goto 0

In this article, we will see an outline on VBA On Error GoTo 0. Error handling is something that has the utmost importance under any programming language and the programmer is expected to know different rules that can be used for error handling. Every programming language has certain rules defined for error handling. In VBA as well we have certain rules which are defined to handle the errors. We ideally use two error handling methods in VBA. “On Error Resume Next” is an error-handling method which neglects the error and then moves to the next line of code execution (this is one sort of error handling rule defined in VBA). On the other side, “On Error GoTo 0” will disable this effect caused by Resume Next and again enable the error window to pop-up as soon as there is an error at the time of debugging.

 

 

In general laymen terms, we can say that, On Error Resume Next enables the error handler and On Error GoTo 0 disables the error handler under VBA. Both of these are contrary to each other. In this article, we are going to work with On Error GoTo 0. It helps to display the error message again on under the given procedure. Otherwise, errors will always be ignored and you’ll never know which part of your code is producing an error. Let’s have an example of  Excel VBA On Error GoTo 0. We’ll see how it affects the code and it’s a compilation as well as debugging.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Examples of Excel VBA On Error Goto 0

Below are the different example of On Error Resume Next in Excel VBA:

You can download this VBA On Error Goto 0 Excel Template here – VBA On Error Goto 0 Excel Template

Step 1: Open a new excel file and open the Visual Basic Editor (VBE) in it by hitting Alt+F11 keys. You can also navigate to the Developers tab and then click on the Visual Basic button to open VBE.

Developer Tab

Step 2: Insert a new module in Visual Basic Editor. Click on Insert placed at the uppermost ribbon of VBE and then select Module in it to be able to add a new module under VBE.

Insert Module

Step 3: Within the newly inserted module, define a new sub-procedure that can hold your macro for the code.

Code:

Sub Onerror_Ex1()

End Sub

VBA On Error Goto 0 Example 1-1

Step 4: Now, define a new variable named DivZ with data type as Long which can be used to evaluate an expression through assignment operator.

Code:

Sub Onerror_Ex1()

Dim DivZ As Long

End Sub

VBA On Error Goto 0 Example 1-2

We are going to use an expression that produces an error under VBA.

Step 5: Use an assignment operator (‘=’) to assign an expression 100 / 0 towards the newly defined variable DivZ.

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
DivZ = 100 / 0

End Sub

Excel VBA On Error Goto 0 Example 1-3

Well, this is an expression that will produce an error under VBA. Since we are trying to divide a number by zero which results in an undefined number. VBA will throw Division by Zero error in this case. Let’s run this code until here and validate if there is an error.

Step 6: Hit F5 or Run button to run this code and see the output. You should get the following error message after running this code.

VBA On Error Goto 0 Example 1-4

If you click on the End button, the system will end this code and if you hit the Debug button, the system will navigate you towards the line where error encountered (It colors the line as yellow).

Now, being a VBA programmer you’ll always be in search of an error handling methodology. Therefore, you’ll use On Error Resume Next in this case.

Step 7: Use the On Error Resume Next error handling method exactly before the line of code that produces an error (i.e. before DivZ = 100/0).

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
DivZ = 100 / 0

End Sub

Excel VBA On Error Goto 0 Example 1-5

This statement will enable the error handling in this case and you will be able to proceed further with the code.

Step 8: Use the If-Else clause to add a logical condition for the DivZ variable. Use the code as If Err.Number = 0 Then

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
DivZ = 100 / 0
If Err.Number = 0 Then

End Sub

If-Else clause Example 1-6

Err.Number property checks If the value for the variable is meeting certain conditions or not. If it meets then it executes the block of code associated with that condition.

Step 9: Now, add a code to be executed if the condition is true. i.e. if the value of DivZ is zero. I will use VBA MsgBox property to get an output if the DivZ has zero Value.

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
DivZ = 100 / 0
If Err.Number = 0 Then
MsgBox "Value for DivZ is zero!"

End Sub

VBA MsgBox Example 1-7

This statement emphasizes that if the DivZ has Zero-value then text under MsgBox will be displayed.

Step 10: Use the Else clause and MsgBox function to add another message when the DivZ is not equaled to zero. Use the code as shown in the screenshot below:

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
DivZ = 100 / 0
If Err.Number = 0 Then
MsgBox "Value for DivZ is zero!"
Else
MsgBox "Something fishy is going on"

End Sub

Else clause Example 1-8

Step 11: Now end the If-Else loop by setting End If clause at the end of the loop so that the system knows when to end this loop and return to the start.

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
DivZ = 100 / 0
If Err.Number = 0 Then
MsgBox "Value for DivZ is zero!"
Else
MsgBox "Something fishy is going on"
End If

End Sub

If-Else loop Example 1-9

Step 12: If you run this code, the system will neglect the error at the assignment of 100/0 to variable DivZ and execute the loop.

VBA On Error Goto 0 Example 1-10

However, this seems to be adding ambiguity to the code. We are directly neglecting the error and moving further towards the execution. This may mislead the user though you have an error, your program doesn’t show it instead of running the code as it is. In order to get rid of this ‘On Error Resume Next’, we can use ‘On Error GoTo 0’. In this code. That will remove the error handling and you’ll be again able to get the error message at the time of execution of code.

Step 13: Add On Error GoTo 0 before the line on which we assigned expression to the variable.

Code:

Sub Onerror_Ex1()

Dim DivZ As Long
On Error Resume Next
On Error GoTo 0
DivZ = 100 / 0
If Err.Number = 0 Then
MsgBox "Value for DivZ is zero!"
Else
MsgBox "Something fishy is going on"
End If

End Sub

VBA On Error Goto 0 Example 1-11

Step 14: If you run this code, you’ll be able to see the error message again for this code.

Error Message Example 1-12

Ideally, On Error GoTo 0 helps us to go to the default error settings or we can say that it helps us remove the error handling if any in the code. This is it from this article. Let’s wrap the things up with some points to be remembered

Things to Remember

  • On Error GoTo 0 is not an error handler method. Instead, it is considered as Error Handler Disabler which disables any Error Handling provided in your code and returns to the default code settings where you see an error message as soon as there is an ambiguity.
  • On Error GoTo 0 is a default behavior under VBA as long as you haven’t provided any error handling method (ex. On Error Resume Next) to neglect the errors.

Recommended Articles

This is a guide to VBA On Error GoTo 0. Here we discuss some useful Example of Excel VBA On Error Goto 0 in Excel along with downloadable excel template. You can also go through our other suggested articles –

  1. VBA RGB
  2. VBA Msgbox Yes/No
  3. VBA Examples
  4. VBA On Error Resume Next
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

Download VBA On Error Goto 0 Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW