The ISERROR function in Excel VBA is a built-in diagnostic tool used to check if a specific expression or variable contains any type of error. It returns a Boolean value: True if an error is detected, and False if the expression is valid or returns a valid value. This function is not an error handler itself—meaning it does not correct the error—but it is essential for writing robust code that can detect, report, and gracefully manage errors before they crash your macro or return incorrect data to the user.
What You'll Need
- Microsoft Excel installed on your computer (Windows or Mac versions included).
- A basic understanding of the VBA Editor, including how to open the Visual Basic for Applications window and insert a Module.
- Access to a spreadsheet with some data to test your code against. Ideally, data that includes text where numbers are expected, or formulas that might result in errors like #DIV/0! or #N/A.
Step-by-Step
- Open the VBA Editor. Press Alt + F11 on your keyboard to launch the VBA editor. If the editor is already open, you can skip this step.
- Insert a New Module. In the VBA editor window, look at the menu bar and click Insert > Module. This creates a blank code window where you will write your VBA script.
- Test the Basic Syntax. In the module window, type the following line of code and press Enter. This is the simplest way to see the function in action. You can run this by pressing F5 while the cursor is on this line, or by opening the Immediate Window (Ctrl + G), typing
Debug.Print IsError(1/0), and pressing Enter. The Immediate Window will print True, confirming that the expression resulted in an error. - Check a Valid Expression. Modify the line from the previous step to test a value that is not an error. For example, type
Debug.Print IsError(100). The Immediate Window will print False, as 100 is a valid number and does not trigger an error. - Use ISERROR in a Conditional Statement. To see how the function is typically used to control the flow of your program, type the following code. This example attempts to calculate a division and checks if an error occurred before proceeding.
- Run the Macro. Save your code (if desired) and press F5 to run the macro. A dialog box will appear stating that an error occurred during calculation. This demonstrates that ISERROR successfully caught the division by zero error.
- Test with Text and Numbers. Data type mismatches are a common source of errors. Modify the divisor variable to be a string, like
Dim divisor As String, and set it to"Five". Run the macro again. The Immediate Window will print True, as attempting to divide a number by a string is a type mismatch error that ISERROR will catch. - Handling VLOOKUP Errors. A very common use case is checking the result of a VLOOKUP or MATCH function, which often returns
#N/Awhen a value is not found. Modify the code to use a VLOOKUP-like logic. Note that for this example, you can use theApplication.Matchfunction, which returns an error if the item is not found. - Verify the Output. Run the macro. If the value "Zebra" is not in your specified range, the macro will correctly identify that the result is an error and display the appropriate message.
Debug.Print IsError(1 / 0)
Sub CheckForError()
Dim result As Variant
Dim divisor As Double
divisor = 0
result = 10 / divisor
' Check if result is an error
If IsError(result) Then
MsgBox "An error occurred during calculation."
Else
MsgBox "Calculation successful: " & result
End If
End Sub
Sub CheckVLookupError()
Dim searchVal As Variant
Dim lookupRange As Range
Dim matchResult As Variant
Set lookupRange = Range("A1:A5")
searchVal = "Zebra"
' Perform the match
matchResult = Application.Match(searchVal, lookupRange, 0)
If IsError(matchResult) Then
MsgBox "The value " & searchVal & " was not found in the list."
Else
MsgBox "Found at position: " & matchResult
End If
End Sub
Common Mistakes
- Using ISERR instead of ISERROR. The ISERR function is very similar to ISERROR, but it only catches errors of type
#VALUE!,#REF!,#DIV/0!,#NUM!, and#NAME?. It does not catch#N/Aerrors. If you are using a function like MATCH or VLOOKUP that returns#N/A, you must use ISERROR, not ISERR. - Using ISERROR on a variable that has already been evaluated. ISERROR checks the result of a specific expression. If you assign a variable a value and then try to check the variable, you are checking the value, not the potential error that might have occurred during assignment. For example,
Dim x As Variant: x = 1/0: Debug.Print IsError(x)will return True, as the variable holds the error. However, simply writingDebug.Print IsError(1/0)is often clearer. - Confusing ISERROR with the On Error Resume Next statement. The On Error Resume Next statement tells VBA to ignore the current error and continue to the next line of code. ISERROR is a function that returns a value based on an evaluated expression. You cannot use ISERROR to suppress errors; it only reports them. You must use On Error Resume Next if you want VBA to ignore errors, and ISERROR if you want to check if an error occurred.
Tips
- Combine with On Error Resume Next. The most common pattern for robust error handling is to combine ISERROR with On Error Resume Next. This allows you to perform a risky operation (like a calculation or a web query) without stopping your macro if it fails, and then use ISERROR to check the result.
- Be specific with your error handling. While ISERROR is a catch-all, it is often better practice to check for specific errors if you know what they are. For example, if you are dividing two values and you know the user might enter zero, you can check for division by zero specifically rather than using a generic ISERROR check.
- Use ISERROR for User Input Validation. When asking users to input data, such as a number for a calculation, use ISERROR to validate their input. This prevents the macro from crashing or producing incorrect results if the user enters text or an invalid symbol.
Troubleshooting
If your code is not behaving as expected when using the ISERROR function, the issue is usually one of three things: the error type you are looking for is not being caught, the expression is not actually producing an error, or the syntax is incorrect.
- ISERROR returns False when you expect True. This is the most common issue. Check the type of error being generated. If you are using a function like MATCH and it returns
#N/A, you must use ISERROR. If you use ISERR, it will return False. Verify that the expression inside the function is actually capable of generating an error under the current conditions. - ISERROR returns True when you expect False. This usually means the expression is producing an error that you were not anticipating. For example, if you are using a variable that might be uninitialized or empty, or if you are performing an operation on a data type that is not compatible with the operation. Check the variable values and data types to ensure they are valid.
- Macro stops immediately. If your macro stops immediately when it reaches the line with IsError, it means the error occurred before the IsError function was even evaluated. This is a classic case of an unhandled error. You must use the On Error Resume Next statement to allow the macro to continue to the IsError line, or you must handle the error using a standard VBA error handler (Try/Catch block).
- Results are inconsistent. If the macro behaves differently depending on the data or conditions, you may be encountering a race condition or a subtle data type issue. Check that all variables are declared with the correct data types, and that the expressions are being evaluated in the correct order.
Frequently Asked Questions
1. What is the difference between ISERROR and ISERR?
ISERROR checks for all error types, including #N/A. ISERR only checks for specific errors like #VALUE!, #REF!, #DIV/0!, #NUM!, and #NAME?. If you are looking for #N/A, you must use ISERROR.
2. Can I use ISERROR inside a formula in a cell?
No, ISERROR is a VBA function and cannot be used directly in an Excel spreadsheet cell. However, you can use the IFERROR function in a cell. The VBA function ISERROR is used to check the result of a calculation before it is written to the cell.
3. Why doesn't ISERROR work with my VLOOKUP?
The VLOOKUP function returns #N/A when a value is not found. If you are checking the result of VLOOKUP using ISERR, it will return False because ISERR does not catch #N/A. You must use ISERROR to correctly identify when a VLOOKUP fails to find a match.
4. How do I handle errors in a loop?
You can use ISERROR inside a loop to check the result of each iteration. If an error is detected, you can either skip the iteration, log the error, or stop the loop early. For example: For i = 1 To 10: If IsError(Calculate(i)) Then Exit For: Next i.
5. Is there a way to suppress errors with ISERROR?
No. ISERROR is a diagnostic function that returns a Boolean value. It does not suppress or ignore errors. To suppress errors, you must use the On Error Resume Next statement. You can combine these two: On Error Resume Next: result = 1/0: If IsError(result) Then MsgBox "Error occurred".
6. Can I use ISERROR to check if a cell contains an error?
Yes, you can use ISERROR to check the value of a cell. For example, Debug.Print IsError(Range("A1").Value) will return True if cell A1 contains an error like #DIV/0!.