Excel VBA IsError: How to Handle Formula Errors
In this article
To use IsError in Excel VBA, you wrap a value or a cell reference within the IsError() function, which returns True if the value is an Excel error (such as #N/A, #VALUE!, #DIV/0!, #REF!, #NUM!, #NAME?, or #NULL!) and False if it is any other type of data. This is primarily used to prevent a macro from crashing when it encounters a cell containing a formula error, as attempting to perform a calculation or string operation on an error value typically triggers a "Type Mismatch" runtime error.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A workbook containing data, preferably with some intentional formula errors (like dividing by zero) to test the code.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the VBA Editor (VBE).
Step-by-Step
- Enable the Developer Tab: If you do not see the Developer tab at the top of your Excel window, right-click any existing tab on the Ribbon, select "Customize the Ribbon," and check the box for "Developer" in the right-hand list. Click OK.
- Open the VBA Editor: Click the "Visual Basic" button on the Developer tab, or press
Alt + F11on your keyboard. This opens the environment where your macros are written. - Insert a New Module: In the VBA Editor, go to the top menu and select
Insert > Module. This creates a blank sheet of code where you can write your procedure. - Declare Your Variables: Start your macro by defining the variables you will use. When working with
IsError, it is critical to use theVariantdata type for the variable holding the cell value. This is because aStringorDoublevariable cannot hold an Excel error value and will crash the code beforeIsErrorcan even check it. - Write the Loop or Reference: Determine which cells you want to check. If you are checking a range, use a
For Eachloop to iterate through every cell in that range. - Implement the IsError Check: Inside your loop, use an
If...Thenstatement. Place the cell value inside theIsError()function. For example:If IsError(cell.Value) Then. - Define the Error Action: Decide what the macro should do if an error is found. Common actions include clearing the cell, replacing the error with a zero, or skipping that row entirely.
- Define the Non-Error Action: Use the
Elsestatement to tell VBA what to do if the cell is healthy. This is where you place your actual data processing logic. - Close the Logic Blocks: Ensure you close your
Ifstatement withEnd Ifand your loop withNext. - Run and Test: Press
F5or click the green "Run" arrow in the toolbar. Observe how the macro handles cells with #N/A or #DIV/0! without stopping the execution.
Common Mistakes
The most frequent mistake when using IsError is declaring the target variable as a specific type, such as Integer, Double, or String. If you write Dim myVal As Double and then assign myVal = Range("A1").Value where A1 contains #DIV/0!, VBA will trigger a "Type Mismatch" error immediately upon assignment. The IsError check happens after the assignment, so the code never reaches the check. Always use Variant for variables that might hold cell errors.
Another common error is confusing IsError with On Error Resume Next. On Error Resume Next is a global instruction that tells VBA to ignore all runtime errors and move to the next line of code. This is dangerous because it can hide serious logic flaws. IsError, conversely, is a logical test that allows you to handle specific data errors gracefully while still maintaining control over the program flow.
Users also often forget that IsError only detects Excel formula errors. It does not detect "empty" cells or cells containing the text "Error" written as a string. To handle empty cells, you must use IsEmpty(); to handle specific text, you must use a standard string comparison.
Tips
To make your code more efficient, combine IsError with a Select Case statement if you need to perform different actions based on the type of error. While IsError only tells you if an error exists, you can use CVErr constants to identify specific errors if you are assigning values manually.
When processing large datasets, avoid reading and writing to the sheet one cell at a time. Instead, load the range into a Variant Array. You can then run the IsError check on the array elements in memory, which is significantly faster than interacting with the worksheet interface repeatedly. Once the array is cleaned of errors, write the entire array back to the sheet in one operation.
If you are using IsError to clean a sheet, consider adding a counter variable. This allows the macro to report exactly how many errors were found and fixed via a MsgBox at the end of the process, providing a helpful audit trail for the user.
Troubleshooting
If your macro is still crashing with a "Type Mismatch" error despite using IsError, check your variable declarations. Ensure that the variable receiving the cell value is Dim x As Variant. If you are passing the cell value directly into a function, ensure that function is also designed to accept a Variant.
If the IsError function is returning False even though you see an error in the cell, check if the cell is actually formatted as Text. If the formula is preceded by an apostrophe (e.g., ' #N/A), Excel treats it as a literal string of text rather than a formula error. In this case, IsError will not detect it because it is technically a valid string.
If the macro runs but doesn't seem to change anything, verify that your range references are correct. Use MsgBox cell.Address inside your loop to ensure the code is actually visiting the cells you intend to check. Also, ensure that you have not accidentally placed the End If before the code that performs the corrective action.
Frequently Asked Questions
What is the difference between IsError and IsEmpty?
IsError checks specifically for Excel error values like #VALUE! or #N/A. IsEmpty checks if a cell has never been initialized or contains no data at all. A cell with a formula error is not "empty," so IsEmpty would return False while IsError would return True.
Can IsError be used inside a worksheet formula?
No, IsError is a VBA-specific function. In a standard Excel worksheet cell, you should use the ISERROR() function. While they perform the same logical task, the VBA version is used in macros, and the worksheet version is used in formulas.
Does IsError detect #N/A specifically?
IsError detects all types of Excel errors, including #N/A. If you need to detect only #N/A and ignore other errors like #DIV/0!, you would need to check the specific error code using the CVErr(xlErrNA) constant or use the IsNA() function if available via a custom wrapper.
Will IsError slow down my macro?
The IsError function itself is extremely fast. However, calling it millions of times while referencing individual cells on a worksheet can be slow. To maintain high performance, load your data into a Variant array first and perform the IsError checks on the array elements.
Can I use IsError to find bugs in my VBA code?
No. IsError is designed to check for errors within cell values (data errors). To handle errors in the VBA code itself (runtime errors), you must use the On Error GoTo [Label] or On Error Resume Next statements.