on error jump to next item in loop

mikey1987

Board Regular
Joined
Mar 22, 2013
Messages
65
Hello,

I got an issue with my "on error go to" statement.

My code looks pretty much like this:

Dim NbrOfBrands as long
Dim counter as long
Dim Mybrand() as variant


(I populate MyBrand somewhere using the redim statement)

For counter = 1 to NbrOfBrands

'If the listed product is not in the sheet, I want to jump to the next one (so skip everything until the next statement)

On Error GoTo ContinueNextProduct
Cells.Find(what:=MyBrand(counter, 1)).Activate
On Error GoTo 0

' Do some stuff

ContinueNextProduct:
next counter

This goes right once :)
I skip one of the products that is not in the list, but the second one gives me an error "Object variable or With block variable not set"

Anyone knows what I'm doing wrong?

Regards,
Mikey
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
maybe substitute your error code with On Error Resume Next
 
Upvote 0
yes but it should skip around 20 lines of code.
So if I encounter an error, I want it to jump to the end of the loop (to the "next counter") and then continue

If I use the on error resume next statement, it would execute all the steps with a brand that doesn't exist.
 
Upvote 0
Got it :)
The trick is to exit the loop, end the on error statement and then jump back in :)

Using the same code:

For counter = 1 to NbrOfBrands

'If the listed product is not in the sheet, I want to jump to the next one (so skip everything until the next statement)

On Error GoTo ContinueNextProduct
Cells.Find(what:=MyBrand(counter, 1)).Activate
On Error GoTo 0

' Do some stuff

NextBrand:
next counter

exit sub

ContinueNextProduct:
on error goto 0
resume NextBrand

end sub
 
Upvote 0
AH yes, those 20 lines of not shown code makes a difference
 
Upvote 0
Yeah, I tried to make it shorter with the "Do some stuff" part :)
Thanks for your help though! Great site!
 
Upvote 0

Forum statistics

Threads
1,224,292
Messages
6,177,716
Members
452,797
Latest member
prophet4see

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