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
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
6 Comments
Matt Ridge
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?
Siddharth Rout
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...
Matt Ridge
I just tried this, and I'm getting a "Label Not Defined" message.
Matt Ridge
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?
Siddharth Rout
Create a new post and ask a question there?
|