on Error goto 0 &on error resume next

L

layla

Hi,
In my macro,before I access the objects,i wrote "on error resume next"
to trap any error that might occur.My questions are:
1) do I need to write somewhere in my code "on Error goto 0", to
disable the error handeler.since with or without using "on error goto
0" in my macro,I do not see any differences.

2) is that enough to use "on error resume next" only once at the
begining of my codes when I want to access the objects,or should I
keep reaping it.since with or without using "on error resume next" in
my macro,I do not see any difference.

Thanks!
 
D

Dave Peterson

It's a very bad idea to use a blanket "on error resume next" line. It hides too
many errors.

But when you know something could cause an error, you can turn off error
checking, run that line, and give error checking back to excel and see if your
line was successful.

Dim wks as worksheet
set wks = nothing
on error resume next
set wks = worksheets("some sheet that may not exist")
on error goto 0

if wks is nothing then
'it's not there
else
'it is there
end if

Turning off error checking should be done for a very limited time.

Chip Pearson has some notes that you may want to read:
http://www.cpearson.com/Excel/ErrorHandling.htm
 
L

layla

Hi agin,
Thanks,for the respose it really helped! Just wonder how exactly "on
error go to 0" works:does it terminate the macro if the error is fatal
what exactly "0"is doing
 
D

Dave Peterson

If you get one of those unhandled run time errors, excel will pop up a dialog
that there was an error.

There'll be options on that dialog to continue, end, debug, or help. (Depending
on your security settings, the debug option may be disabled.)

Chip's site shows you how to handle those unexpected errors. If you're righting
code for someone else (or even for yourself???), you don't want them to see that
dialog.

But while I'm debugging my code, seeing that dialog doesn't bother me. It just
means I have to clean up my code more.
 

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