Capture error messages in VBA

To capture error messages in VBA, use this code:

CODE:

'At the beginning of your code

On error goto err_handler

'Put this code somewhere in your code (most people put it at the bottom of the sub/function

err_handler:

'Note: this next line is for when you want to capture a specific error message and alert the user to it.

If Err.Number = -2147467259 And Err.Description = "Numeric field overflow." Then

MsgBox "An error has occurred. You have an invalid supervisor. Please change.", vbCritical, "Error"

Else

'display a message to user when it is an error you do not know anything about

If err.number <> 0 then

MsgBox "Error " & Err.Number & " " & Err.Description, vbCritical, "Error"

End if

End if

Exit Sub