Error handling exception

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Exploring the use of Error handling in VBA and stuck with following:

Code:
On Error GoTo myContinue
'code here to find an item and cut & paste it into a previously created worksheet "Exceptions"
'a flag to highlight if exceptions found which is called upon later in the macro
NoExceptionsFlag = False
myContinue: Worksheets("Exceptions").delete

I would like the macro to delete the worksheet "Exceptions" if no exceptions are found, otherwise carry on with the rest of the macro WITHOUT deleting the worksheet "Exceptions"

I've tried modifying the last two codes of line above to:

Code:
myContinue: NoExceptionsFlag = False
If Not NoexceptionsFlag Then: Worksheets("Exceptions").delete

but the line myContinue is always exceuted, regardless of whether the Error is found (i.e. the item to find is found or not)

Please can someone enlighten me?

Thanks in advance,
Jack
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code is always executed in order. All that happens in your code is that you skip the intervening lines if there is an error. If not, you still run through the following code.
I would also point out that:
1. You should only use an On Error Goto label syntax to jump to a specific error handler section at the end of your code, not to simply skip a block of code.
2. You should always have a Resume statement of some kind in the error handler to reset error handling (unless you plan to simply exit the routine).
 
Upvote 0
Thanks Rory, what would be the syntax for RESUME in this case, i.e.

Code:
On Error GoTo myLabel
'stuff
myLabel
Resume Next
'more stuff

I may need to spend more time on this to fully grasp, so apologies if I'm asking anything simple!

Jack
 
Upvote 0
I think you missed my point 1... ;)
 
Upvote 0
As I clicked send I realised that's what you wrote - i.e. it goes at the end, d'oh!

Ok, let me rephrase, how would I code:

IF something is found Then
do some stuff
Else
do some other stuff
End IF

Since I need to allow for instances when the item can't be found...
 
Upvote 0

Forum statistics

Threads
1,216,458
Messages
6,130,757
Members
449,588
Latest member
accountant606

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