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 IFError
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 IFError

By Ashwani JaiswalAshwani Jaiswal

VBA IFERROR

VBA IFERROR

A written code many times gives the error and chances of getting an error in complex error are quite high. Like excel has IFERROR function which is used where there are chances of getting an error. IFERROR changes the error message into other text statements as required and defined by the user. Similarly, VBA IFERROR functions same as the IFERROR function of Excel. It changes the error message into the defined text by the user.

This error mostly occurs when we perform any mathematical function or if we are mapping any data which is in a different format. IFERROR handles many errors, some of them are:

#VALUE!, #N/A, #DIV/0!, #REF!, #NUM!, #NULL! And #NAME?

Example #1

Watch our Demo Courses and Videos

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

Now this IFERROR function can also be implemented in VBA. Now to compare the results from Excel with VBA IFERROR we will insert a column where we will apply VBA IFERROR statement as shown below.

You can download this VBA IFERROR Excel Template here – VBA IFERROR Excel Template

For this, we consider the same data and insert another column where we will perform VBA IFERROR. Go to VBA window and insert a new Module from Insert menu as shown below.

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,389 ratings)

Insert Module

Now in a newly opened module, write Sub-category with any name. Here we have given the name of the operating function as shown below.

Code:

Sub VBA_IfError()
End Sub

VBA IFERROR Example 1-1

Now with the help of ActiveCell, we will select the first reference cell and then directly use IFERROR Formula with reference cell (RC) -2 divided by -1 cell numbers. Which means we are dividing cell A by cell B in the first argument. And in the second argument write any statement which we want in place of error. Here we will use the same state which we have seen above i.e. “No Product Class”.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"

End Sub

VBA IFERROR Example 1-2

Now select the range cell which would be our denominator. Here we have selected cell C2.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"
    Range("C2").Select

End Sub

VBA IFERROR Example 1-3

Now to drag the formula to below cells where we need to apply IFERROR till the table has the values.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"
    Range("C2").Select
    Selection.End(xlDown).Select

End Sub

VBA IFERROR Example 1-4

Now to come to the last cell of the column with the help of command Range where we need to drag the IFERROR formula. Here our limit ends at cell D6.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"
    Range("C2").Select
    Selection.End(xlDown).Select
    Range("D6").Select

End Sub

VBA IFERROR Example 1-5

Now to drag the applied IFERROR down to all the applicable cells select the Range from End (or Last) cell to Up to the applied formula cell by End(xlUp) command under Range.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"
    Range("C2").Select
    Selection.End(xlDown).Select
    Range("D6").Select
    Range(Selection, Selection.End(xlUp)).Select

End Sub

VBA IFERROR Example 1-6

As we do Ctrl + D to drag the up cell values to all selected cells in excel, here in VBA, Selection.FillDown is used to fill the same up cells values in all selected cells.

Code:

Sub VBA_IfError()

    ActiveCell.FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1],""No Product Class"")"
    Range("C2").Select
    Selection.End(xlDown).Select
    Range("D6").Select
    Range(Selection, Selection.End(xlUp)).Select
    Selection.FillDown

End Sub

VBA IFERROR Example 1-7

This completes the coding of VBA IFERROR. Now run the complete code by clicking on the play button as shown in below screenshot.

VBA IFERROR Example 1-8

As we can see above, in column D starting from Cell 2 to 6 we got our results.

There is another format and way to apply IFERROR in VBA. For this, we will consider another set of data as shown below. Below we have sales data of some products in column A and quality sold in column B. And we need map this data at cell F2 with reference to product type Laptop. Now data in the first table has #N/A in cell B3 with for Laptop quantity sold out which means that source data itself has an error. And if we look up the data from the first table to Cell F2 we will get same #N/A here.

Example 2-1

For this open a new module in VBA and write Subcategory with the name of a performed function.

Code:

Sub VBA_IfError2()

End Sub

Example 2-2

Select range where we need to see the output or directly activate that cell by ActiveCell followed by dot (.) command line as shown below.

Code:

Example 2-3

Now select FormulaR1C1 from the list which is used for inserting any excel function.

Example 2-4

Now use the same formula as used in excel function in VBA as well.

Code:

Sub VBA_IfError2()

    ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],R[-1]C[-5]:R[3]C[-4],2,0),""Error In Data"")"

End Sub

Example 2-5

Now select the cell where we need to see the output in Range. Here we have selected cell F3.

Code:

Sub VBA_IfError2()

    ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],R[-1]C[-5]:R[3]C[-4],2,0),""Error In Data"")"

    Range("B8").Select

End Sub

Example 2-6

Now compile and run complete code using F5 key or manually and see the result as shown below.

Example 2-7

As we can see in the above screenshot, the output of product type Laptop, from first table #N/A is with error text “Error In Data” as defined in VBA code.

Pros of VBA IFERROR Function

  • It takes little time to apply the IFERROR function through VBA.
  • Result from Excel insert function and VBA IFERROR coding, both are the same.
  • We can format or paste special the applied formula as well to avoid any further problem.

Cons of VBA IFERROR Function

  • IFERROR with this method, referencing the range of selected cell may get disturbed as the table has limited cells.

Things To Remember

  • We can record the macro and modify the recorded coding as per our need.
  • Always remember to save the file as Macro-Enabled Excel, so that we can use the applied or created macro multiple times without any issue.
  • Make sure you remember to compile the complete code before ending the command function. By this, we can avoid or correct any error incurred.
  • You can apply the created code into a button or tab and use it by a single click. This is the quickest way to run the code.
  • Best way to avoid any changes after running the code is to paste special the complete cells so that there will not be any formula into the cell.

Recommended Articles

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

  1. VBA Arrays
  2. VBA Number Format
  3. VBA Find
  4. VBA Do While Loop
1 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