bruderbell
Active Member
- Joined
- Aug 29, 2006
- Messages
- 374
Folks,
I want to build a macro that will load up a VBA macro (permutations()) with all the combinations of x items from a list of y possible items. To start out, I'm trying for all combinations of 4 from a list of 10. I know that I can do nested for loops, but I want a dynamic macro.
I'm trying to learn about recursive programming. A bit of googling led me down that path. I'm not sure if it is the right way to get this done, but below is my attempt so far.
The code starts with 1, stores it, calls itself, adds a 2 (i'm delimiting with hyphens), calls itself, gets a 3, calls itself and gets a 4, then writes out the 4 digit combination. Then I want the code to go back and continue working on the other calls. So it should go back to 1-2-3- and try 5, then write out 1-2-3-5, but it seems that when I am passing the variable it isn't remaining in the old instances. So when the macro gets to 1-2-3-4 and writes out the value, then goes back to try 5 in the fourth element, it isn't starting with 1-2-3...it keeps 1-2-3-4 then tries 5, but then it has more elements than my limit.
Does this make any sense to anyone?
I want to build a macro that will load up a VBA macro (permutations()) with all the combinations of x items from a list of y possible items. To start out, I'm trying for all combinations of 4 from a list of 10. I know that I can do nested for loops, but I want a dynamic macro.
I'm trying to learn about recursive programming. A bit of googling led me down that path. I'm not sure if it is the right way to get this done, but below is my attempt so far.
The code starts with 1, stores it, calls itself, adds a 2 (i'm delimiting with hyphens), calls itself, gets a 3, calls itself and gets a 4, then writes out the 4 digit combination. Then I want the code to go back and continue working on the other calls. So it should go back to 1-2-3- and try 5, then write out 1-2-3-5, but it seems that when I am passing the variable it isn't remaining in the old instances. So when the macro gets to 1-2-3-4 and writes out the value, then goes back to try 5 in the fourth element, it isn't starting with 1-2-3...it keeps 1-2-3-4 then tries 5, but then it has more elements than my limit.
Does this make any sense to anyone?
Code:
Sub test()
Dim x As Integer, quantity As Integer, a As String, permutations()
x = Range("A1:B10").Rows.Count
quantity = Range("E2").Value
ReDim permutations(1 To 2, 1 To (WorksheetFunction.Fact(x) / WorksheetFunction.Fact(x - quantity)))
q = perm("-", x, quantity, permutations, 1)
End Sub
Function perm(a As String, x As Integer, quantity As Integer, permutations, i As Integer)
For i = i To x - 1
'First we check if the proposed value (i) is already in the proposed solution (a)
If UBound(Split(a, i)) < 1 Then
If UBound(Split(a, "-")) < quantity + 1 Then
'find correct value
a = a & i & "-"
a = perm(a, x, quantity, permutations, i) 'may want to pass i+1
Else
n = 1
'Next we figure out where in the array we will put the result
Do While permutations(1, n) <> Empty
n = n + 1
Loop
'write value out to array
permutations(1, n) = a
a = "-"
Exit Function
End If
End If
Next i
End Function
Last edited: