• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Error message

I have attached an error message, has anyone any code that if that particular error arises i can give the user an msgbox "xyz" and then exits sub

Thanks
Seamus
Doc4.doc
0
Seamus2626
Asked:
Seamus2626
  • 2
  • 2
1 Solution
 
Frank WhiteCommented:
I think the attachment might have been mixed up. Seems to be from a previous question, in fact.

As for catching the error, here's an excellent tutorial on VBA error handling..

As shown there, you can use On Error GoTo ErrHandler: and If Err.Number = # statements to catch a particular error and do some action. You can also use use both your own error handler and the default one by adding "On Error GoTo 0" and then "Resume" statements at the end of your error handling block, which reverts to normal error-catching and then retries the line that caused an error.
0
 
Seamus2626Author Commented:
Oops sorry, correct attachment
Doc4.doc
0
 
Frank WhiteCommented:
The way I would go about this is:

'Function/sub declaration, variables, etc.
On Error GoTo ErrHandler:

'Your code including the code that produces that error.

ErrHandler:
    MsgBox Err.Number
    On Error GoTo 0
    Resume
'End function/sub

Open in new window


Then, run it once and the message box should tell you the right number to put in the following code:

'Sub declaration, variables, etc.
On Error GoTo ErrHandler:

'Your code including the code that produces that error.

ErrHandler:
    If Err.Number = ((**ERRORNUMBER**)) Then 
        MsgBox "Not enough memory left!"
        On Error GoTo 0
        Exit Sub
    End If
    On Error GoTo 0
    Resume

End Sub

Open in new window

0
 
Rory ArchibaldCommented:
Can I ask what you are doing to get that error?
0
 
Seamus2626Author Commented:
Thanks.

I was able to plug that in

Seamus
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now