Exiting a nested for cycle

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I sometimes have for loops nested in for loops, but once i find my answer id like all of the loops to exit, not just the current one. My current code looks like:
Code:
For Each item1 In fulllist1
    For Each item2 In fullList2
        If item2.Text = "hit" Then
            done = True
            Exit For
        End If
    Next
    If done Then Exit For
Next
Is there a better approach to do it?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
you can put them all in a subroutine and use exit sub, this is what I do for multiple loops. You do need to declare variables at the module level though!
 
Upvote 0
I try to use global's as the last last resort, but that could indeed work, nice idea thank you! :)
 
Upvote 0
Hello guys,

I sometimes have for loops nested in for loops, but once i find my answer id like all of the loops to exit, not just the current one. My current code looks like:
Code:
For Each item1 In fulllist1
    For Each item2 In fullList2
        If item2.Text = "hit" Then
            done = True
            Exit For
        End If
    Next
    If done Then Exit For
Next
Is there a better approach to do it?
Have you tried using goto ?
 
Upvote 0
Are the 2 loops/list connected?

Sorry not sure what you mean by that, but let's imagine i'm browsing through a 2d array that has random numbers inside and i want to exit as soon as even a single copy of the number i'm looking for is found
 
Upvote 0
Hi,

Untested but could you set a boolean variable e.g. bExitLoop to False, then set it to True just before exiting the inner loop, then in outer loop check the value of bExitLoop and if True, exit the outer loop?

Hope this helps,

Eric
 
Upvote 0
Whilst it's very much down to personal preference, I tend to do it the same way you have.
I avoid using GoTo wherever possible.
 
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,652
Members
452,664
Latest member
alpserbetli

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