How do I make the code below recursive? I'm trying to create all combination of the array. This gives all combinations where the length is exactly 5. I want it to give all combinatoin where the length is 1 to the ubound of array s. So for a length of 3 there would be 3 nested loops, for length of 8 there would be 8 nested loops and so on.
Ultimately what I want to accomplish is to have an array filled with numbers and find the sum of every possible combination and pick the combination that has a sum that is closet to a target number without going under, but I thought I'd start here first.
VB Code:
Sub CreateCombos() Dim s(8) s(1) = "A" s(2) = "B" s(3) = "C" s(4) = "D" s(5) = "E" s(6) = "F" s(7) = "G" s(8) = "H" lLen = 5 For i1 = 1 To (UBound(s) - lLen) + 1 For i2 = i1 + 1 To (UBound(s) - lLen) + 2 For i3 = i2 + 1 To (UBound(s) - lLen) + 3 For i4 = i3 + 1 To (UBound(s) - lLen) + 4 For i5 = i4 + 1 To (UBound(s) - lLen) + 5 Debug.Print s(i1) & s(i2) & s(i3) & s(i4) & s(i5) Next i5 Next i4 Next i3 Next i2 Next i1 End Sub
Any help?![]()




Reply With Quote