'------ start of code -------
Sub ErrorDemo1()

    ' Establish error-handling for this procedure.  
    ' The statement below tells VBA to transfer control to 
    ' the statement labeled "My_Error_Handler" if an error
    ' occurs.

    On Error GoTo My_Error_Handler

    ' The bulk of the procedure code goes here.

    '** The following statement will raise an error. **

    Debug.Print 1 / 0     ' <--- ERROR: can't divide by zero.

Exit_Point:
    ' Put code here that should be executed before leaving 
    ' the procedure, regardless of whether an error occurs or
    ' or not.  For example, you might close recordsets,
    ' display messages, close forms, etc.

    ' The Exit Sub statement below is required to keep the 
    ' natural sequence of code execution from continuing into 
    ' the error-handler, even if no error has occurred.

    Exit Sub

My_Error_Handler:
    ' In this error-handler, one might check the error number to see
    ' exactly what error has occurred and display an appropriate
    ' message, or take some special action.

    Select Case Err.Number

        Case 11
            MsgBox "You tried to divide by zero, you dummy!"
            
        Case Else
            MsgBox _
                "An unexpected error has occurred.  The system's description " & _
                    "of this error is:" & vbCr & vbCr & _
                    "Error " & Err.Number & ": " & Err.Description, _
                vbExclamation, _
                "Unexpected Error"

    End Select

    ' Resume execution at the procedure's "clean-up" location.
    Resume Exit_Point

End Sub
'------ end of code -------