Why VBA goes to error handling code when there is no error?

I have writen some code in VBA (Excel) with error handling labels. It worked fine until I recently notice the error handling code gets executed everytime, not just when an error occurs. Does anybody know why this happens? Thanks.

Here's a trivial test case where both msgboxes would pop up.

Sub example()
    On Error GoTo err_handle
    MsgBox "OK!"
err_handle:
MsgBox "not OK"
End Sub

You want to add an Exit Sub to your routine:

Sub example()
    On Error GoTo err_handle
    MsgBox "OK!"
    Exit Sub
    err_handle:
    MsgBox "not OK"
End Sub

Look here for a full explaination.


It's because you aren't returning out of the subroutine after the first message box the (OK) one. After that is shown the next line of code is executed which is the "not ok" message.

You could either exit the subroutine early before your error handler label (ExitSub) or goto the end of the subroutine on success (bypassing the "error" routine)


need to add exit sub else the program continues to execute the label as it is the part of the code