vba on error code example
Example 1: vba error handling message
Dim Msg
On Error GoTo errorhandling
errorhandling:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.Helpfile, Err.HelpContext
Err.Clear
End If
Example 2: vba on error goto label
Private Sub TestMe()
Dim m As New Collection
On Error GoTo CleanFail
Debug.Print 1 / 0
CleanExit:
Set m = Nothing
Debug.Print "Clean exit"
Exit Sub
CleanFail:
Debug.Print Err.Description
Err.Clear
Resume CleanExit
End Sub