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'?
 
Wouldn't this work

for i = 1 to 5
For j = 1 To 7

'do whatever

Next j
Next i

or am I missing something?
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
For i being 1 to 4, they may want more than 7 values of j processed, and also there might be other code in the For i loop that they want skipped when i = 5 and j = 7.
 
Upvote 0
For i being 1 to 4, they may want more than 7 values of j processed, and also there might be other code in the For i loop that they want skipped when i = 5 and j = 7.
If it was just skipping a process I'd see where you were coming from, but both loops are being terminated. Also if the first was true, you could set a condition to control the upper limit of J when I was >4.
 
Upvote 0
Also if the first was true, you could set a condition to control the upper limit of J when I was >4.

I don't see how that relates to what you posted?
 
Upvote 0
I don't see how that relates to what you posted?
I think what Weaver meant is something like this this...

Code:
For i = 1 To 5
    For j = 1 To #
        '   possible code here
        If i = 5 Then Exit For
        '   possible code here
    Next j
Next i
where the # symbol could be any large number more than 7.
 
Upvote 0
Maybe, but that's not at all the same as:
Code:
for i = 1 to 5
For j = 1 To 7

which was my point. It also wouldn't stop any code after the 'Next j' from processing, so not a true exit from both loops.
 
Upvote 0

Forum statistics

Threads
1,225,149
Messages
6,183,188
Members
453,151
Latest member
Lizamaison

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