EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Error Handling Functions VBA On Error Statements
Secondary Sidebar
VBA Error Handling Functions
  • VBA Error
    • VBA IsError
    • VBA On Error
    • VBA IFError
    • VBA 1004 Error
    • VBA Subscript out of Range
    • VBA OverFlow Error
    • VBA Error Handling
    • VBA On Error Resume Next
    • VBA On Error Goto
    • VBA On Error GoTo 0
    • VBA Type Mismatch

VBA On Error Statements

By Ashwani JaiswalAshwani Jaiswal

VBA On Error

VBA On Error Statements

VBA On Error is an easy method for handling unexpected exceptions in Excel Macros.It is known that we cannot write code without any error. Sometimes writing big code may give us an error even at the time of compiling. To avoid this kind of situation, we add an Error Message which, instead of giving us the right answer or error code it will show us the message with the error code. That would look like we got the output of our calculation, but it is the error code that will get imprinted.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,035 ratings)

How to Use Excel VBA On Error Statements in Excel?

There are 3 ways of Error in VBA. Let’s understand different ways with some examples.

Watch our Demo Courses and Videos

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

Example #1

The first error type is a Code compilation error which comes when a code is undeclared or impossible variables. To understand more, we will use a simple mathematical expression of the divide. For this, go to the Insert menu of VBA and select Module as shown below.

Insert Module

Now open Subcategory and add any name as we are using On Error, so we have named it as same.

Sub OnError()

End Sub

VBA On Error Example 1-1

Now define any 2 or 3 Integers. Here we have taking X and Y as Integers.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

End Sub

VBA On Error Example 1-2

Now, as discussed above, we will calculate division mathematical expression. For X, we will put a character in Numerator and divide it 0. And Y will be 20/2, which is complete numbers.

Sub OnError()

Dim X As Integer, Y As Integer

X = Test / 0

Y = 20 / 2

End Sub

VBA On Error Example 1-3

Now run the code by using the F5 key or clicking on the play button as shown below. We will get Run-time error 6, which shows Text over number error.

VBA On Error Example 1-4

Now to overrule this error, we will add one line On Error Resume Next before we write the mathematical code. It will jump the error code, but we will not able to see the outcome of the second mathematical code. This only hides the error message of various codes lines, as shown below. Now try to run the code as well.

Sub OnError()

Dim X As Integer, Y As Integer

On Error Resume Next

X = Test / 0

Y = 20 / 2

MsgBox X
MsgBox Y

End Sub

VBA On Error Example 1-5

Now to overrule this error, we will add one line On Error Resume Next before we write the mathematical code. It will jump the error code, but we will not able to see the outcome of the second mathematical code. This only hides the error message of various codes lines, as shown below. Now try to run the code as well.

VBA On Error Example 1-6

Example #2

In this example, we will consider that mathematical division which gives infinite result, but in coding, it will #DIV/0 result. To demonstrate this, we will consider one more integer Z along with X and Y in a subcategory, as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

End Sub

VBA On Error Example 2-1

Now frame all the integers X, Y, and Z with a mathematical expression of divide and to print it use MsgBox function in VBA of each integer’s result.

Below for Integer X, we have divided 10 by 0, 20 by 2 and 30 by 4.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

X = 10 / 0

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

VBA On Error Example 2-2

Now run the code using the F5 key or manually, as shown below.

VBA On Error Example 2-3

As we can see in the above screenshot, Run-time error 11, which means the error is related to the number. Now to overcome this, add one line On Error Resume Next before mathematical expression as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

On Error Resume Next

X = 10 / 0

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

VBA On Error Example 2-4

Now, if we run the code, we will get zero for the first integer X and for Y and Z, we will get respective division answers as shown below.

 VBA On Error Example 2-5                 

Example #3

The second type of error is seen when we enter incorrect data entry in code. For this, we will consider 3 integers, X, Y, and Z, by opening the Subcategory in VBA as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

End Sub

Example 3-1

Now also consider the same mathematical division which we have seen in the above example.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

X = 10 / 0

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

Example 3-2

If we run the code, we will get the same error message of Run-time error 11.

Example 3-3

Now to overrule this error, use text On Error GoTo with the word “ “Result to skip the error message and get the output which works fine, as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

On Error GoTo ZResult:

X = 10 / 0

Y = 20 / 2

ZResult:
Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

Example 3-4

Now run the code again. We will get the same result as the previous example.

Example 3-5

On Error GoTo ZResult helps us to directly jump of mentioned result point integer as we did for integer Z.

Example #4

In the third type of error, when we run the code and VBA is not able to understand the line of code. This can be done with the help of code On Error Resume Next along with MsgBox Err.Number. Consider the same data as used in the above examples. We will again see the same 3 integers X, Y, and Z, as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

End Sub

Example 4-1

And for printing the result output, allow message boxes for all Integers as output.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

X = 10 / 0

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

Example 4-2

Now, if we run the complete code, then we will get an error message of mathematical error Run time error 11.

Example 4-3

Now to overrule this error, we will use On Error Resume Next.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

On Error Resume Next

X = 10 / 0

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

Example 4-4

And run the code. This will give a use result on a valid mathematical line, as shown below.

Example 4-5                               

Now further add ZResult code line before Z integer division mathematical expression and add MsgBox Err.Number code line at the end of code as shown below.

Sub OnError()

Dim X As Integer, Y As Integer, Z As Integer

On Error Resume Next

X = 10 / 0

Y = 20 / 2

ZResult:
Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

MsgBox Err.Number

End Sub

Example 4-6

Now run the code by using the F5 key or by pressing the play button, as shown below.

Example 4-7

As we can see in the above screenshots. The first message box has 0, which is overruling of incorrect mathematical expression. 2nd and 3rd have a division result of Y and Z integers. And last message box has run time error code 11, which is probably the error code of X integer’s division expressions.

Pros of VBA On Error

  • We can calculate any mathematical formula even if it is incorrect.
  • For bigger coding structures where there are chances of having an error, using these methods may give correct result even among the line of codes.
  • This gives a better result as compared to the result obtained from normal excel calculations.

Things to Remember

  • Always save the file in a Macro-Enabled Excel file so that we can use created VBA code many and multiple times.
  • Always compile the written code before implementing with any excel requirement.
  • If needed, assign the written code to any button so that we can quickly click on the button and run the code.

You can download this VBA On Error Excel Template here – VBA On Error Excel Template.

Recommended Articles

This has been a guide to Excel VBA On Error. Here we discussed how to use VBA On Error Statement along with some practical examples and a downloadable excel template. You can also go through our other suggested articles–

  1. VBA TRIM
  2. VBA Number Format
  3. VBA Do While Loop
  4. VBA Find
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Watch our Demo Courses and Videos

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

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

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

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more