Exiting a double for loop

GeneralShamu

Board Regular
Joined
Jul 6, 2007
Messages
127
I have the following setup

For i = 1 To #
For j = 1 To #
if (i = 5 & j = 7)
[exit both for loops]
end if

Next j
Next i

Does anyone know how to do that? If I put 'Exit For' in the if body, would it still read code up to the 'End If'?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
As soon as execution hits Exit For it exits the loop.

So anything after that won't get executed.

What is it you want to do anyway?
 
Upvote 0
It's an interesting question - I assume you want out of the the outer loop too. Not on Excel right now to test but here's a hack:
Code:
 i = 1 To 100
    For j = 1 To 100
        If i = 5 And j = 7
            [COLOR="Red"]i = 100[/COLOR]
	    Exit For
        End If
    Next j
Next i

This is another possibility - a little more intricate to code but more clear should somebody later on be examining your code:
Code:
Exit_Loops = False
i = 1
j = 1
Do While i <=100 And Not Exit_Loops
    Do While j <= 100 And Not Exit_Loops
        
	If i = 5 And j = 7
            Exit_Loops = True
	Else
	    'Do Something Here
        End If
    
    j = j + 1
    Loop
i = i + 1
Loop

I have a habit of using Do While about 99% of the time (so I always know to look for my conditions at the top of the block), but maybe it would also be a good fit in a Loop While structure too.
 
Last edited:
Upvote 0
I have the following setup

For i = 1 To #
For j = 1 To #
if (i = 5 & j = 7)
[exit both for loops]
end if

Next j
Next i
Does anyone know how to do that? If I put 'Exit For' in the if body, would it still read code up to the 'End If'?[/QUOTE]
Your subject line indicates you want to jump out of both loops from inside the inner one... you will not be able to do that with a single Exit For statement. Exit For takes you out of a single loop only... the loop it is executed in. Given your example, which I think could probably be structured differently (depending on ultimate goal and what those loops are actually doing), this is probably what you would need to do...

Code:
For i = 1 To #
  For j = 1 To #
    If (I = 5 & j = 7) Then Exit For
    [other code]
  Next
  If (I = 5 & j = 7) Then Exit For
  [other code]
Next j
Of course the [other code] indicators could be located before the indicated If..Then statements (or even on both sides of it), again, depending on what your code is doing.
 
Last edited:
Upvote 0
Does anyone know how to do that? If I put 'Exit For' in the if body, would it still read code up to the 'End If'?
Your subject line indicates you want to jump out of both loops from inside the inner one... you will not be able to do that with a single Exit For statement. Exit For takes you out of a single loop only... the loop it is executed in. Given your example, which I think could probably be structured differently (depending on ultimate goal and what those loops are actually doing), this is probably what you would need to do...

Code:
For i = 1 To #
  For j = 1 To #
    If (I = 5 & j = 7) Then Exit For
    [other code]
  Next
  If (I = 5 & j = 7) Then Exit For
  [other code]
Next j
Of course the [other code] indicators could be located before the indicated If..Then statements (or even on both sides of it), again, depending on what your code is doing.[/QUOTE]

I had thought about doing it this way but it became quite messy. Let me think about it some more.
 
Upvote 0
I think Rick's suggestion is probably the clearest and easiest.

I don't see how it's messy, unless the criteria for exiting the loop get more complicated.

What is it you are looping through anyway?
 
Upvote 0
I had thought about doing it this way but it became quite messy. Let me think about it some more.
My guess is your coding could be constructed somewhat differently to avoid this "messiness", but without knowing what is supposed to be going on inside each loop, it is impossible to say more. Perhaps For..Next loops are not what you should be using... maybe Do..Loops would handle your situation better and be less messy.
 
Upvote 0
You could also use a GoTo statement.

Code:
Sub test()

For i = 1 To 10
    For j = 1 To 10
        If i = 5 And j = 7 Then GoTo mylabel
            ActiveSheet.Cells(i, j).Value = i + j
    Next j
    
Next i

mylabel:
    MsgBox "now after 'mylabel' label"

End Sub
 
Upvote 0
Code:
For i = 1 To #
  For j = 1 To #
    If (I = 5 & j = 7) Then Exit For
    [other code]
  Next
  If (I = 5 & j = 7) Then Exit For
  [other code]
Next j

Thank you Rick Rothstein; I ended up going with this after looking through it this morning.

@gregtx81
I like the idea; I just feel GoTo statements can easily create a web of mess (recalls QBasic).
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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