General question re Error Handling

T

terry w

hello experts

This is just a general question for my own education. I've dabbled in a
fair amount of Access VBA in the past, but have just started learning about
Excel VBA. In Access, we would use Error Handling in almost every sub, but
In Excel, error Handling seems to be the exception rather than the rule. I
can't even find much about it in the Excel VBA help, and there are relatively
few references to it in this forum. What gives?

Just Curious
Terry W.
 
R

Robert Flanagan

Terry, I use it all the time. My favorite approach is:

Public Const bDebug as boolean = true or false (depending on need)

Sub Main_Routine_With_Error_Trap()
if not bdebug then on error goto etrap
Main_Procedure
exit sub
etrap:
msgbox "Woops " & err.desription
End Sub

The above, with a better message obviously, traps all errors except those
in procedures in user forms. I thus tend to not run any elaborate user form
code other than input validation.

If I'm debugging code, I set bDebug to True. When I'm done, I set to False.
And, I put a If statement in the auto_close to warn me if bDebug is true.

Some use of On Error Resume Next to avoid errors and then testing the result
to see what actually happened. For example

Dim wB as workbook
set wB = Nothing
On error resume next
set wB = workbooks("My workbook.xls")
on error goto 0
if wb is nothing then msgbox "My workbook.xls is not open"

Robert Flanagan
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 

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

Similar Threads


Top