4

Is there a way to say if Error 1004 shows up, show message "Message" and If Error 9, show message "Message2" instead of generic non-descriptor geek speak message for end user?

1 Answer 1

7

What you are trying to do is called Error Handling.

See this Example. You can trap the Error Number using Err.Number

Sub Sample()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    Select Case Err.Number
        Case 9
            MsgBox "Message1"
        Case 1004
            MsgBox "Message2"
    End Select
End Sub

FOLLOWUP

Sub Sample1()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    MsgBox GetErrMsg(Err.Number)
End Sub

Sub Sample2()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    MsgBox GetErrMsg(Err.Number)
End Sub

Function GetErrMsg(ErNo As Long) As String
    Select Case ErNo
        Case 9
            GetErrMsg = "Message1"
        Case 1004
            GetErrMsg = "Message2"
        Case Else
            GetErrMsg = "Message3"
    End Select
End Function
Sign up to request clarification or add additional context in comments.

6 Comments

Sweet and simple, the best type of code, thanks :) Just one question, does this have to go into each section, or can this be created as it's own module?
It has to go into each section. But you can write a common procedure on what you want to do for example... see the updated code...
I just tried this, and I'm getting a "Label Not Defined" message.
I got it working, just about a minute ago, don't need help there, but I figured out another "issues" if I have a long message, how do I break it up?
Create a new post and ask a question there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.