Table of Contents
The IFERROR function in VBA is a powerful tool that allows users to handle and manipulate errors in their code. It is used to check for errors in a specific formula or expression and return a specified value if an error is encountered. This function is especially useful when dealing with large and complex datasets, as it helps to avoid unexpected errors and ensures smooth execution of code.
To use the IFERROR function in VBA, the syntax is as follows: IFERROR (value, value_if_error). The first argument, “value”, is the expression or formula that needs to be evaluated. The second argument, “value_if_error”, is the value that will be returned if an error is encountered.
Some examples of using the IFERROR function in VBA include:
1. Checking for division errors: If a formula contains a division operation, there is a possibility of encountering a #DIV/0! error. By using the IFERROR function, we can specify a value to be returned if this error occurs, such as “N/A” or “0”.
2. Handling missing data: When working with large datasets, there may be missing data that can result in errors. The IFERROR function can be used to check for these errors and return a specific value, such as “N/A” or “Not Available”.
3. Evaluating multiple conditions: The IFERROR function can also be nested within other functions to handle multiple conditions. For example, we can use IFERROR within an IF statement to check for errors and perform different actions based on the outcome.
In conclusion, the IFERROR function in VBA is a valuable tool for handling errors and ensuring the smooth execution of code. Its usage can prevent unexpected errors and provide more control over data manipulation.
Use IFERROR in VBA (With Examples)
You can use the following basic syntax to use the IFERROR function in VBA to display a specific value in a cell if an error is encountered in an Excel formula:
Sub IfError()
Dim i As Integer
For i = 2 To 11
Cells(i, 4).Value = WorksheetFunction.IfError(Cells(i, 3).Value, "Formula Error")
Next i
End Sub
This particular example checks if each cell in rows 2 through 11 of the third column in the current sheet has an error value.
If an error value is encountered, a value of “Formula Error” is assigned to the corresponding cell in the fourth column, otherwise the numerical value from the third column is assigned to the corresponding value in the fourth column.
The following example shows how to use this syntax in practice.
Example: How to Use IFERROR in VBA
Suppose we have the following dataset in Excel that shows the total revenue and units sold of some product at different stores:

Column C uses a formula to divide Revenue by Units Sold to come up with Revenue per Unit.
However, notice that the formula produces a value of #DIV/0! in some cells where we attempt to divide by zero.
Suppose we would like to create a new column that instead displays “Formula Error” for those cells.
We can create the following macro to do so:
Sub IfError()
Dim i As Integer
For i = 2 To 11
Cells(i, 4).Value = WorksheetFunction.IfError(Cells(i, 3).Value, "Formula Error")
Next i
End SubWhen we run this macro, we receive the following output:

The values in column D either show the results from the formula in column C or they display a value of “Formula Error” if an error value is shown.
Feel free to change “Formula Error” in the IfError method in the code to instead display whatever value you would like when an error is encountered.
Cite this article
stats writer (2024). How can I use the IFERROR function in VBA and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-iferror-function-in-vba-and-what-are-some-examples-of-its-usage/
stats writer. "How can I use the IFERROR function in VBA and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-iferror-function-in-vba-and-what-are-some-examples-of-its-usage/.
stats writer. "How can I use the IFERROR function in VBA and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-iferror-function-in-vba-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How can I use the IFERROR function in VBA and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-iferror-function-in-vba-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How can I use the IFERROR function in VBA and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the IFERROR function in VBA and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
