If there's an error then Exit the Sub!

J

Julia

Hi,

This is simple for all of you but I can't get it to work. Instead of the
user seeing a VBA error message, I'd like for the sub to end or for it to
skip down a few lines of code. How do I do this?

Thanks!
 
V

Vince

Sub Sample()
On Error GoTo ErrorDescription: ' Any error results in the program runnin
down to errordescription:

Dim i As Integer

i = "asjdaskdjasdjas" ' Error produced here (assinging string to integer)

GoTo ex:
ErrorDescription:
MsgBox "sorry dude, I have a problem: " & Err.Description

ex:
End Sub

OR

Sub Sample()
On Error resume next: ' No error message will be shown! Just goes to the
next ling
Dim i As Integer

i = "asjdaskdjasdjas" ' Error produced here (assinging string to integer)
msgbox "Hi" ' Hi will be output!


GoTo ex:
ErrorDescription:
MsgBox "sorry dude, I have a problem: " & Err.Description

ex:
End Sub

You can also do an Instr(err.description,"Type mismatch")<>0 or something
like that and trap all the errors and have something done for each case....
 
J

Julia

Thank you
-----Original Message-----
Sub Sample()
On Error GoTo ErrorDescription: ' Any error results in the program runnin
down to errordescription:

Dim i As Integer

i = "asjdaskdjasdjas" ' Error produced here (assinging string to integer)

GoTo ex:
ErrorDescription:
MsgBox "sorry dude, I have a problem: " & Err.Description

ex:
End Sub

OR

Sub Sample()
On Error resume next: ' No error message will be shown! Just goes to the
next ling
Dim i As Integer

i = "asjdaskdjasdjas" ' Error produced here (assinging string to integer)
msgbox "Hi" ' Hi will be output!


GoTo ex:
ErrorDescription:
MsgBox "sorry dude, I have a problem: " & Err.Description

ex:
End Sub

You can also do an Instr(err.description,"Type mismatch")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top