1

I am trying to iterate through every possible combination of integers in an array. Each array position has its own minimum and maximum possible integers. I have been able to do this with the following code:

'loop through each possible integer in each cell in the array from corresponding minimum to corresponding maximum
For g1 = MinG1 to MaxG1
    For g2 = MinG2 To MaxG2 
        For g3 = MinG3 To MaxG3 
            For g4 = MinG4 To MaxG4 
                For g5 = MinG5 To MaxG5 
                        '... and so on until...
                            For g36 = Min36 To MaxG36
                            Next
                        '... and so on until...                        
                Next
            Next
        Next
    Next
Next

Obviously it's slow and ugly, but it works and cycles through every possible iteration of integers within each corresponding constraint. I am trying to simplify it greatly using arrays and doing the following:

'loop through each cell in the array
Dim i As Long
Dim j As Long

For i = UBound(GRange) To LBound(GRange) Step -1   

    'loop through each possible integer in current cell from corresponding minimum to corresponding maximum
    For j = MinGRange(i, 1) To MaxGRange(i, 1)
    GRange(i, 1) = j
    Next

Next

My problem is that it cycles through each cell from minimum to maximum value, but doesn't repeat when the next cell iterates (ie in my ugly code, G36 will return to its minimum and start iterating again when G35 iterates to the next number). I have not found a solution to show me how to get that to happen.

I don't want a 35 stage nested loop. What is it I am missing?

FinProg
  • 23
  • 4
  • https://stackoverflow.com/questions/19780016/vba-write-all-possible-combinations-of-4-columns-of-data/19780307#19780307 – Tim Williams Apr 12 '18 at 05:01

0 Answers0