Run-time error

  • Thread starter Thread starter Francis Hookham
  • Start date Start date
F

Francis Hookham

Run-time error '1004':

Application-defined or object-defined error



this appears when no Print_Area is defined



ActiveSheet.Names("Print_Area").Delete

ActiveSheet.Names("Print_Titles").Delete



I think I have seen that an error message can be turned off before a likely
error and on again after - dangerous if not carefully used.



How should I handle this possible error?



Francis Hookham
 
You are correct taht error handling should be used judisciously. That being
said what you are trying to do can be handled safely something like this

OnError Resume Next
ActiveSheet.Names("Print_Area").Delete
ActiveSheet.Names("Print_Titles").Delete
On Error Goto 0
 
You had me guessing for s minute or two - I pasted in your code without
looking carefully at it.

'OnError' in first line! All's well now - many thanks.

Francis
 
Error handler (OnError) is ok in this instance because you are ok if it fails
and there is nothing to worry about if it does.

Time to hop on my soap box... The error handler is not there to address poor
code. IMO if you can avoid an error you are much better off than trying to
deal with it after the fact. That being said there are times when your code
may generate errors because you are looking for something that may or may not
exist (such as in your case). You can use the error handler here if either
you don't care if the code fails (such as your case) or you are trying to set
an object that you will check later to see if it was successfully created.
 
Just adding to Jim's excellent response, it is *critical* to understand that
an On Error does NOT in any way fix the error. I've seen more code than I
care to remember in which the original programmer assumed that On Error
would somehow fix the problem. It doesn't. It just ignores it.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
In message <[email protected]>
at 11:54:00 on Fri, 14 Sep 2007, Jim Thomlinson
Time to hop on my soap box... The error handler is not there to address poor
code. IMO if you can avoid an error you are much better off than trying to
deal with it after the fact. That being said there are times when your code
may generate errors because you are looking for something that may or may not
exist (such as in your case).
A classic example is finding out the ubound of an uninitialised dynamic
array
 

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

Back
Top