Error Handling Code for Worksheet Not Loaded

NBVC

Well-known Member
Joined
Aug 31, 2005
Messages
5,828
Can someone help with an error-handling code for the following please:

I have a custom macro button in the toolbar. If the user clicks that button when there is no worksheet loaded at all (ie. when Excel is just opened), then I would like a message to pop up to alert them of that instead of getting an error message and asking them to debug the code.

The button is linked to a macro in personal.xls

Thanks.
 
NBVC,

Did you try the error handling solution I proposed? It is pretty straightforward, and is a little more elegant then Resume Next.

Hi jm14,

I was afraid you'd ask that. I wanted to see if Erik's simpler method would have worked....

But I just tried your solution...and it works great! Thanks...

Just because I want to learn this stuff well, why is this considered more elegant than the Resume Next? Is it just because your are trapping a specific error? What if another error occurs other than 1004, then I would have to include these codes obviously in the If statement? Can't I just trap any error and continue on?

How exactly does that "On Error Resume Next" work....If I place it at top of my code, will it avoid any error at any time during the running of the code or only if it happens at the beginning... or do I need to place it at several points within the code where I think an error can occur.?
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
NBVC,

The way that it works, is that anytime it encounters an error, it will jump to the error checking. If it finds error 1004, it returns the custom message box we set-up. Otherwise it goes to the Else clause in the error checking which will return the error number and description (so you do not have to set up IF clauses for each conceivable error, just the ones you want to "intercept").

The reason why that it is more elegant than Resume Next is because Resume Next will ignore any error instead of just that specific error. That, as you probably know, can be dangerous, as you might be ignoring other errors that you really don't want to ignore, and your code might not function properly.

When you get past the point of where the error might occur in your code, you can get the error checking back to the Excel default with the command:
On Error GoTo 0

So, you will often see code in this format:
Code:
On Error GoTo Err_Chk
' Perform steps here
On Error GoTo 0

Let me know if you have any other questions.
 
Upvote 0
Thanks very much, jm14, for this useful information....


I will get this all in one day :)
 
Upvote 0

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top