# Replace nested loops with one variable loop statement

#### xun_zi

##### New Member
Hello,

I have a permutation problem requiring many nested loops that are structurally identical.

E.g.

I work with the range A1:C50
The code will write values to column A and B. Column C contains a formula evaluating A and B.
The code starts by assigning an iteration value from 1-20 to column A / row 1, then an iteration value from 1-4 to column B / row 1.
If C = TRUE then the code goes on to the next row. If all rows 1-50 in column C = TRUE than the problem is solved.
Whenever C = FALSE, column B iterations will first be exhausted, then column A iterations, before moving back to the previous row and continuing the iteration there.

I know how to do this by writing each loop separately, i.e.

For i_A_1 = 1 to 20
If Cells(1,3) = FALSE then
GoTo Line_NextIteration_1
End if

etc.

For i_B_1 = 1 to 4

etc.

Line_NextIteration_50:
Next i_B_50
Next i_A_50

etc.

Line_NextIteration_1
Next i_B_1
Next i_A_1

However, I wonder if there's away to avoid all this code since each loop is structurally identical and use variable iterations, e.g. from x = 1 to 50, i_A_x = "i_A_" & x. However, I'm not sure how I would accomplish the descending Next statements with this approach? Thanks.

### 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.
Edit: Should be

For i_A_1 = 1 to 20
If Cells(1,3) = FALSE then
End if

For i_B_1 = 1 to 4
GoTo Line_NextIteration_1

etc.

Line_NextIteration_50:
Next i_B_50
Next i_A_50

etc.

Line_NextIteration_1
Next i_B_1
Next i_A_1

However, I wonder if there's away to avoid all this code since each loop is structurally identical and use variable iterations, e.g. from x = 1 to 50, i_A_x = "i_A_" & x. However, I'm not sure how I would accomplish the descending Next statements with this approach? Thanks.[/QUOTE]

Edit 2: Sorry, messed that up. Should be:

For i_A_1 = 1 to 20

For i_B_1 = 1 to 4
If Cells(1,3) = FALSE then
GoTo Line_NextIteration_1
End if

etc.

Line_NextIteration_50:
Next i_B_50
Next i_A_50

etc.

Line_NextIteration_1
Next i_B_1
Next i_A_1

Replies
1
Views
318
Replies
1
Views
489
Replies
8
Views
1K
Replies
3
Views
332
Replies
4
Views
1K

1,196,098
Messages
6,013,441
Members
441,766
Latest member
ixruiz

### 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.

### Which adblocker are you using?

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

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