How to Use On Error Resume Next in Excel VBA for Error Handling

When you’re writing macros in Excel using VBA, it’s normal to run into errors. These might happen if a worksheet is missing, a value is wrong, or an object isn’t available at the moment the code runs. Instead of letting the macro stop with an error message, you can use On Error Resume Next to skip over the problem and keep your code going.

In this article, we’ll look at how On Error Resume Next works in Excel VBA. You’ll learn when to use it, how to use it safely, and how to avoid common mistakes. We’ll also walk through clear examples so you can handle errors without letting them crash your macro or cause confusion.

Key Takeaways

Steps to use On Error Resume Next to ignore a runtime error in Excel VBA:

➤ Open the VBA Editor (press Alt + F11).
➤ Insert a new module and paste the following code:

Sub FillSalaries_OnError()
    Dim k As Long
    For k = 2 To 11                       ' D2:D11 hold lookup names
        On Error Resume Next
        Cells(k, 5).Value = WorksheetFunction.VLookup( _
                           Cells(k, 4), Range("A:B"), 2, False)
        If Err.Number <> 0 Then           ' lookup failed
            Cells(k, 5).ClearContents     ' leave cell blank
            Err.Clear                     ' reset before next loop
        End If
        On Error GoTo 0                   ' restore normal handling
    Next k
End Sub      

➤ Run the macro in the Excel sheet..
➤ Names found in A1:B11 write their salaries to E2:E11. Missing names (Gayathri, Karanveer, Maya) stay blank.
➤ No error dialogs interrupt the process; the loop completes all ten rows smoothly.

overview image

Download Practice Workbook
1

Use On Error Resume Next to Skip a Missing-Shape Error in Excel VBA

When writing macros that delete objects like buttons or shapes, it’s common to run into an error if the object doesn’t exist. If VBA tries to delete a shape that isn’t on the worksheet, it throws a run-time error and stops the execution of your code. To avoid this, we can use the On Error Resume Next statement, which tells VBA to ignore the error and continue with the next line of code. This is especially useful when you’re working with optional elements like buttons that may or may not be present.

We’ll demonstrate using one worksheet called Data. It contains an existing shape, btnRefresh (The Refresh Data Button), but lacks btnClean. The macro attempts to delete the btnClean shape. btnClean triggers an error without proper handling.

Use On Error Resume Next to Skip a Missing-Shape Error in Excel VBA

With On Error Resume Next, the macro skips the missing‑shape error, deletes btnRefresh if present, and finishes smoothly. Execution continues; users see no interruption, and subsequent unexpected errors still appear.

Steps:

➤ Press  Alt  +  F11  to open the VBA Editor, then go to Insert >> Module to add a new module.
➤ Paste the following code to first run the version without error handling:

Sub DeleteMissingShape_NoHandler()
    Sheets("Data").Shapes("btnClean").Delete
End Sub

Use On Error Resume Next to Skip a Missing-Shape Error in Excel VBA

➤ Go back to the sheet. Press  F5  to run the macro.

VBA will show this message: Run-time error ‘-2147024809 (80070057)’: The item with the specified name wasn’t found.

Use On Error Resume Next to Skip a Missing-Shape Error in Excel VBA

This is because btnClean does not exist.

➤ Now, update the macro with proper error handling:

Sub DeleteMissingShape()
    On Error Resume Next                    ' Ignore error if shape is missing
    Sheets("Data").Shapes("btnClean").Delete
    On Error GoTo 0                         ' Turn error handling back on
End Sub

Explanation
On Error Resume Next skips the error caused by trying to delete a shape that doesn’t exist.
On Error GoTo 0 restores normal error behavior after the risky line.
➧ This method helps you avoid interruptions while still keeping future error detection intact.

Run the macro again. This time, there will be no error pop-up.


2

Detect a Missing Sheet with Err.Number After On Error Resume Next

Using On Error Resume Next by itself skips errors but tells you nothing about what went wrong. By checking Err.Number immediately afterwards, you can spot the specific problem and respond, whether that means showing a friendly message, creating a missing object, or logging the issue.

For this demo we still have just one worksheet, Data. There is no sheet named Sales in the workbook, so any direct reference to “Sales” will raise run‑time error 9. That predictable failure is perfect for showing how Err.Number lets us branch into custom recovery code.

When you run the finished macro, it quietly detects the missing Sales sheet, displays a message, creates the sheet, clears the error, and continues. If the sheet already exists, the macro simply grabs it and moves on, with no interruptions either way.

Steps:

➤ Open the VBA Editor (Alt + F11), choose Insert >> Module, and paste this first version without handling:

Sub GetSalesSheet_NoHandler()
    Dim ws As Worksheet
    Set ws = Worksheets("Sales")    ' Sales sheet does not exist
End Sub

Detect a Missing Sheet with Err.Number After On Error Resume Next

➤ Run it (F5). You’ll hit Run‑time error 9: Subscript out of range because the sheet is missing. Click End.

Detect a Missing Sheet with Err.Number After On Error Resume Next

➤ Now paste the handled version below and run it:

Sub GetSalesSheet()
    Dim ws As Worksheet
    On Error Resume Next           ' skip the next error
    Set ws = Worksheets("Sales")   ' may fail
    If Err.Number <> 0 Then        ' check if it did fail
        MsgBox "Sales sheet not found. Creating it now.", vbInformation
        Err.Clear                  ' reset Err so later errors show up
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sales"
        Set ws = Worksheets("Sales")   ' try again, this time it works
    End If
    On Error GoTo 0                ' restore normal error handling
End Sub

The macro finishes without a run‑time error.


3

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop

For tasks, errors can appear inside loops. Placing On Error Resume Next inside the loop skips failures only for that row, so the macro finishes processing remaining items without interruption.

Our Data sheet contains master salary table in A1:B11 and lookup names in D1:E11. Some names like Gayathri, Karanveer, and Maya are missing from the master, intentionally creating VLOOKUP errors.

With the handler inside the loop, valid salaries fill column E, and unmatched names stay blank. No error dialog appears; the macro runs and completes processing all ten employees successfully.

Steps:

➤ Open the VBA Editor (Alt + F11), choose Insert >> Module, and paste this test macro without error handling:

Sub FillSalaries_NoHandler()
    Dim k As Long
    For k = 2 To 11                         ' rows with lookup names
        Cells(k, 5).Value = WorksheetFunction.VLookup( _
                            Cells(k, 4), Range("A:B"), 2, 0)
    Next k
End Sub

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop

➤ Run it in the Excel sheet (F5). When the code reaches Gayathri in row 4, VBA stops with Run‑time error 1004: Unable to get the VLookup property of the WorksheetFunction class. Click End.

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop

If you go back to the Excel sheet afterwards, you will notice that the script only fills the first two fields, and stops at the third field which contains the name ‘Gayathri’. The employee Gayathri is not present in the main lookup table at A1:B11, which triggers the error.

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop

➤ Now add the loop‑level handler and run again:

Sub FillSalaries_OnError()
    Dim k As Long
    For k = 2 To 11
        On Error Resume Next
        Cells(k, 5).Value = WorksheetFunction.VLookup( _
                           Cells(k, 4), Range("A:B"), 2, False)
        If Err.Number <> 0 Then
            Cells(k, 5).ClearContents
            Err.Clear                       ' reset so the next pass is clean
        End If
        On Error GoTo 0
    Next k
End Sub

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop

Explanation
On Error Resume Next is placed inside the loop, so it applies only to the current iteration.
On Error GoTo 0 resets handling, ensuring later errors are not hidden.
Err.Clear ensures each row starts with a clean slate and the loop fills every available salary.
➧ The approach keeps the loop robust, processing every employee without breaking on predictable lookup failures.

➤ This time, the macro finishes. Salaries appear for matching names, and rows with missing names remain blank, no pop‑ups interrupt the loop.

Handle Loop Errors with On Error Resume Next Inside a VLOOKUP Loop


Frequently Asked Questions

What does On Error Resume Next actually do?

It tells VBA to skip the line that caused an error and continue running normally, instead of showing an error message. It only ignores one specific runtime error at a time.

When should I use On Error Resume Next instead of structured error handling?

Use it for short, predictable error-prone actions, like deleting optional objects, where you prefer to silently skip one failure, not for full routines.

How do I avoid hiding serious bugs with On Error Resume Next?

Immediately check Err.Number after the risky line and reset error handling with On Error GoTo 0. This ensures only expected errors are ignored.

Why might On Error Resume Next not work in a loop?

If repeated in each iteration, it may suppress errors globally, leading to hard-to-find bugs. Instead, apply it before each risky call and turn it off right afterwards.


Wrapping Up

Handling errors in VBA helps keep your macros running smoothly, even when something unexpected happens. The On Error Resume Next statement is helpful when you want to skip over known or minor errors without stopping your code. In this article, we looked at different ways to use this feature from simple inline skipping to using Err.Number and Select Case, each method gives you a different level of control depending on your needs.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo