To ignore errors in Excel VBA, use the On Error Resume Next statement. This command tells the VBA compiler to skip the line of code that causes a runtime error and immediately proceed to the next executable line, preventing the macro from crashing and displaying a debug dialog box. This is most effective when you expect a specific, non-critical error—such as attempting to delete a sheet that might not exist—and want the script to continue regardless.
What You'll Need
- Microsoft Excel installed on a Windows or macOS computer.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the Visual Basic for Applications (VBA) Editor.
- A macro or subroutine where you are encountering runtime errors.
Step-by-Step
- Open the VBA Editor: Press
Alt + F11on your keyboard (orOption + F11on Mac) to open the Visual Basic Editor window. - Locate Your Procedure: In the Project Explorer on the left, double-click the module or sheet object containing the code you wish to modify.
- Identify the Error-Prone Line: Find the specific line of code that is causing the crash. For example, if you are using
Sheets("Data").Selectbut the "Data" sheet is sometimes missing, this is where the error occurs. - Insert the Ignore Command: Place the cursor on the line immediately above the problematic code and type
On Error Resume Next. This instructs VBA to stop reporting errors from this point forward. - Add the Problematic Code: Ensure your original line of code follows the ignore statement. VBA will now attempt to execute this line; if it fails, it will simply jump to the next line without alerting you.
- Reset Error Handling: It is critical not to leave
On Error Resume Nextactive for the entire macro, as this hides genuine bugs. Immediately after the risky line of code, typeOn Error GoTo 0. This disables the custom error handling and restores the default VBA behavior of alerting you to errors. - Test the Macro: Return to Excel and run your macro. If the error condition is met, the code should now glide past the error without interruption.
Common Mistakes
One of the most frequent mistakes is using On Error Resume Next at the very top of a long subroutine and never turning it off. While this might seem like a quick way to make a macro "just work," it creates a dangerous environment where critical failures—such as a failed database connection or a corrupted range reference—go unnoticed. This leads to "silent failures" where the macro finishes running, but the data is incorrect or incomplete, and you have no way of knowing where the process broke down.
Another common error is confusing On Error Resume Next with On Error GoTo 0. Beginners often think GoTo 0 means "go back to the start of the code." In reality, On Error GoTo 0 is a specific command that tells VBA to stop ignoring errors and return to the standard error-reporting mode. Failing to include this reset means that every subsequent error in your script will be ignored, making debugging nearly impossible.
Users also frequently forget to check if the line they ignored actually succeeded. If you ignore an error when selecting a worksheet, but the rest of your code depends on that worksheet being active, the subsequent lines will execute on the wrong sheet, potentially overwriting important data. Always verify the result of a risky operation after ignoring the potential error.
Tips
For more robust code, combine On Error Resume Next with an If statement to handle the error gracefully. Instead of just ignoring the error, you can check the Err object. For example, after attempting to select a sheet and ignoring the error, use If Err.Number <> 0 Then to execute a specific set of instructions (like creating the missing sheet) before calling Err.Clear and On Error GoTo 0.
Use the On Error Resume Next approach sparingly. It is best reserved for "expected" errors—situations where the error is a valid logical path (e.g., checking if a file exists by trying to open it). For unexpected errors, a structured error handler using On Error GoTo ErrorHandler is far superior, as it allows you to log the error and notify the user in a professional manner rather than simply skipping the problem.
When debugging a complex macro, comment out your On Error Resume Next lines by adding an apostrophe (') at the start of the line. This allows the errors to surface again, helping you pinpoint exactly which lines are failing so you can fix the root cause rather than just masking the symptom.
Troubleshooting
If your macro is still crashing despite using On Error Resume Next, ensure that the statement is placed before the line that causes the error. VBA executes code sequentially; if the error occurs on line 10, but your ignore statement is on line 11, the crash will happen before the instruction is ever read.
If the macro runs without errors but the output is wrong, you are likely experiencing a silent failure. To fix this, remove all On Error Resume Next statements and run the code again. This will force VBA to stop at the exact line where the failure occurs. Once you identify the line, examine the variables using the Locals Window (View > Locals) to see why the operation is failing. You may find that a variable is empty or a range is not defined as expected.
In some cases, On Error Resume Next may not catch certain types of "Compile Errors" (such as syntax errors or missing variable declarations when Option Explicit is used). These errors are caught by the editor before the code even starts running. You must fix these manually by correcting the spelling or declaring the variables; no runtime error handler can ignore a compile-time error.
Frequently Asked Questions
What is the difference between Resume Next and GoTo 0?
On Error Resume Next tells VBA to ignore any errors and move to the next line. On Error GoTo 0 tells VBA to stop ignoring errors and go back to the default behavior of showing an error message box.
Will this stop my macro from crashing entirely?
It stops runtime errors from triggering the debug dialog, but it does not fix the underlying problem. If the code depends on the failed line to function, the macro may still produce incorrect results or crash later on.
Can I use this to ignore all errors in my entire workbook?
You cannot apply a single global setting for the whole workbook. You must place the error handling statements within each individual subroutine or function where you want them to apply.
Does On Error Resume Next slow down my macro?
No, there is no significant performance penalty for using these statements. However, the time spent debugging a "silent failure" later on usually far outweighs any perceived benefit of ignoring errors.
Is it better to use On Error Resume Next or On Error GoTo Label?
On Error GoTo Label is generally better for professional applications because it allows you to redirect the code to a specific error-handling section at the bottom of your sub, where you can provide a helpful message to the user.
What happens to the Err object after Resume Next?
The Err object stores the details of the last error that occurred. Even if you use Resume Next, the Err.Number will be updated to the error code of the failed line, allowing you to check and react to the error programmatically.