VBA's On Error GoTo statement will not execute your error-handling routine if you use the Exit Sub or Exit Function statement directly before your error label. The code must reach the label naturally; it cannot jump over it. The fix is to place a conditional check or a specific Exit statement that points to the label, rather than ending the procedure entirely.

What You'll Need

  • A computer running Microsoft Windows with Microsoft Excel, Access, or another Office application installed.
  • The Visual Basic Editor (VBE) open on your screen.
  • A basic understanding of VBA procedures, specifically Sub and Function definitions.

Step-by-Step

  1. Open the VBA Editor. In your Office application, press Alt + F11 to launch the Visual Basic Editor window.
  2. Locate your Module. In the Project Explorer pane (usually on the left), double-click the module containing the code where the error occurs.
  3. Identify the structure. Look at the sub-procedure where the error handling is failing. It should look something like this:
Sub MyBadProcedure()
    On Error GoTo ErrorHandler
    
    ' Some code that might fail
    Dim x as Integer
    x = 1 / 0 ' Division by zero error
    
    ' This Exit Sub is the problem
    Exit Sub 
    
ErrorHandler:
    MsgBox "An error occurred: " & Err.Description
End Sub
  1. Understand why it fails. In the example above, the line Exit Sub is executed before the code can ever reach the label ErrorHandler:. The runtime engine encounters the exit command and terminates the process immediately, ignoring the label entirely.
  2. Insert a flag or conditional check. The most robust way to solve this is to ensure the error handler is only reached if an actual error occurs. You can use a variable to track the flow. Modify your code to look like this:
Sub MyFixedProcedure()
    Dim ErrorOccurred As Boolean
    ErrorOccurred = False
    
    On Error GoTo ErrorHandler
    
    ' Some code that might fail
    Dim x as Integer
    x = 1 / 0 ' Division by zero error
    
    ' No unconditional Exit Sub
    
    Exit Sub
    
ErrorHandler:
    ErrorOccurred = True
    Resume Next
    
    ' Clean up code that runs even on error
    If ErrorOccurred Then
        MsgBox "An error occurred: " & Err.Description
    End If
End Sub
  1. Use the Resume Next technique. A simpler approach, often used for logging, is to let the code continue after the error. This is done by adding Resume Next at the end of your ErrorHandler label. This tells VBA to skip the line that caused the error and resume with the line immediately following it.
Sub MyLoggingProcedure()
    On Error GoTo ErrorHandler
    
    ' Code here
    Sheets("Sheet1").Range("A1").Value = "Hello" 
    
    ' Normal exit
    Exit Sub
    
ErrorHandler:
    ' Log the error to a sheet or file
    Sheets("ErrorLog").Range("A1").Value = Now()
    Sheets("ErrorLog").Range("B1").Value = Err.Description
    
    ' Resume execution at the line after the error
    Resume Next
End Sub
  1. Check for typos in label names. Ensure the name on the ErrorHandler: line matches the name in your On Error GoTo line exactly. Even a missing space or a different case (e.g., Error_Handler: vs ErrorHandler:) will cause the label not to be found.

Common Mistakes

  • Missing labels. Forgetting to actually write the label line (e.g., ErrorHandler:) is a common oversight. The compiler will raise a syntax error before the code even runs.
  • Using Exit Sub inside the error handler. If you have an Exit Sub line inside your ErrorHandler label, the procedure will exit immediately after handling the error, which might be the desired behavior, but it can mask further errors if not handled carefully.
  • Label scope issues. If your error label is defined inside a nested function but your On Error GoTo points to it from the parent function, it will not work. Labels must be at the same procedural level as the error handler.

Tips

  • Use meaningful names. Name your labels descriptively, such as ErrorHandler_ForDataImport or Err_LogToFile. This helps you identify which block of code is handling the error when you are debugging complex applications.
  • Keep error handlers short. Ideally, your error handler should log the error and then exit the procedure. If the handler contains complex logic that could itself fail, you risk a cascading error that is difficult to debug.
  • Clear the error object. After handling an error, it is good practice to set Err.Clear if you are about to continue executing code that might trigger another error, to prevent the old error number from interfering with your logic.

Troubleshooting

If you have implemented the steps above and the code still does not jump to your error handler, check the following:

  • Breakpoints. If you have set a breakpoint on the line immediately following the error, the debugger will stop there instead of jumping to the label. Use F8 to step through the code manually to see exactly where it stops.
  • Debug mode. If the VBE is currently in Break mode (e.g., because a previous error stopped the execution), any new error will not trigger the handler until you run the code again. Press F5 to continue execution.
  • Compiler errors. Ensure that your code compiles without errors. If you have a syntax error (like a missing parenthesis) earlier in the procedure, the entire sub will not run, and the error handler will never be reached.
  • Call stack. Sometimes errors occur in a called procedure. Ensure that the calling procedure has the On Error GoTo statement active, or that the called procedure has its own error handler that passes the error back up.