Replace nested loops with one variable loop statement

xun_zi

New Member
Joined
Dec 13, 2016
Messages
3
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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]
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,245
Members
448,555
Latest member
RobertJones1986

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