Re: If there's an error then Exit the Sub!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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....


"Julia" <Julia@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DF4E4C74-F26E-40D5-992E-25825E2B7430@xxxxxxxxxxxxxxxx
> 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!
>
>


.



Relevant Pages