Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaks in vb

Status
Not open for further replies.

aurbo

Technical User
Sep 2, 2002
7
CA
Greetings

How would you prevent Debug mode from popping up if an UNEXPECTED error occured during a macro function?

I would rather have the user see an error msg pop up telling them an error occured which stopped the function than have the debugger appear and let them guess and maybe try to edit the script themselves.

Steve
 
Have a look at the on error statement in excel VBA help

works like this:

Sub whatever()
on error goto errHandle
do stuff
do stuff
do stuff

errHandle:
msgbox "An error has occured" & chr(10) & "Please contact ....."
exit sub
end sub
Rgds
~Geoff~
 
Geoff answers your question perfectly, but in a more general sense, you can also use
Code:
On Error Resume Next
which will cause all errors to be ignored and no messages will be displayed at all.

This makes debugging a rather blind art, though, so keep these lines commented until you've finished everything else. Obviously you don't want to use this approach in every situation, but it is nice knowing that you have the option of dismissing all errors without exception (slight pun, get it?)
 
You programmers and your exceptions! [wink]

Another use of the On Error Resume Next construct is in a function where the fact that an eror occurred conveys useful information but you don't care about the error itself, per se. The following Excel example is from John Walkenbach and demonstrates a function to determine if all cells in a range use Data Validation:

Code:
Funtion HasValidation(r) as Boolean
Dim x

On Error Resume Next
x = r.Validation.Type
IF Err.Number = 0 Then HasValidation = True Else HasValidation = False

End Function

Here, the
Code:
x = r.Validaation.Type
will result in an error if one or more cells in range r do not use validation.

Regards,
Mike
 
Sub whatever()
on error goto errHandle
do stuff
do stuff
do stuff
exit sub'add this here otherwise you get msg wrongfully
errHandle:
msgbox "An error has occured" & chr(10) & "Please contact ....."
exit sub
end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top