Exiting nested For Each

chuckjitsu

New Member
Joined
Apr 24, 2015
Messages
48
Office Version
  1. 365
  2. 2016
Hello. I searched here for an answer and found that setting a Boolean value to trigger the exit seemed the best way to go. That said, I wasn't quite sure of the mechanics/syntax of it. Given the dummy code below, how would I use a Boolean to exit the var3 For Each if a condition is met and return to the var1 For Each? (I just used ellipses instead of writing out the entire For Each piece)

VBA Code:
Sub grot()


For Each var1 in...
For each var2 in...
For each var3 in...

Next var3
Next var2
Next var1


End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this:
Code:
Sub grot1()

Dim isExit As Boolean
For Each var1 In v1 '...
For Each var2 In v2 '...
For Each var3 In v3 '...
    '...
    If isExit = True Then
        isExit =  False
        GoTo ContinueVar1
    End If
Next var3
Next var2
ContinueVar1:
Next var1
End Sub
or:
Code:
Sub grot2()

Dim isExit As Boolean
For Each var1 In v1 '...
For Each var2 In v2 '...
For Each var3 In v3 '...
    '...
    If isExit = True Then Exit For
Next var3
    If isExit = True Then
        isExit = False
        Exit For
    End If
Next var2
Next var1
 
Upvote 0
Solution
Try this:
Code:
Sub grot1()

Dim isExit As Boolean
For Each var1 In v1 '...
For Each var2 In v2 '...
For Each var3 In v3 '...
    '...
    If isExit = True Then
        isExit =  False
        GoTo ContinueVar1
    End If
Next var3
Next var2
ContinueVar1:
Next var1
End Sub
or:
Code:
Sub grot2()

Dim isExit As Boolean
For Each var1 In v1 '...
For Each var2 In v2 '...
For Each var3 In v3 '...
    '...
    If isExit = True Then Exit For
Next var3
    If isExit = True Then
        isExit = False
        Exit For
    End If
Next var2
Next var1
Hi Phuoc. That'll do the trick. Thanks for the solution.
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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