Recursive VBA combinations

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,948
Office Version
  1. 365
Platform
  1. Windows
I have the data below. The data is in E1:J15.

I need to get all possible combinations of the names in the table.

But, the number of names from each column must correspond with the numbers in row 1.

So the first result would be

Andy, Doug, Eddie, Allen, Calvin, Eric, Greg, Graham, Bill

Then it would cycle through all the P6 entries. So the next one would be

Andy, Doug, Eddie, Allen, Calvin, Eric, Greg, Graham, Miguel

then

Andy, Doug, Eddie, Allen, Calvin, Eric, Greg, Graham, Tristan and so on until all possible combinations were listed.

I

123111
P1P2P3P4P5P6
AndyDougAllenGregGrahamBill
TomEddieCalvinTylerMasonSanchez
RyanFrankEricGaryRobbieMiguel
BenJeremyRandallTravisAdamTristan
MatthewJonathanVincentAustinRandyMorgan
JohnnyMarkTravisScottDustinTJ
PhilipMattSteveRichardDanKyle
BrianShaneJordanCharlesPhilMike
DerekThomasRuebenJasonMattLeroy
SamWillieAndrew
NateSebastian
Josh
Caleb

I have the following recursive code from an old project

Code:
Sub Combo()
Dim vArray() As Variant, vResult() As Variant
Dim lRow As Long
Dim r As Range

Set r = Range("A1:A3")

For i = 0 To r.Rows.Count - 1
    ReDim Preserve vArray(0 To i)
    vArray(i) = r(i + 1)
Next

vResult = vArray

perm1 vArray, vResult, 0, lRow

End Sub

Sub perm1(vArray As Variant, vResult As Variant, lInd As Long, lRow As Long)
Dim j As Long
Dim res As Double

For j = 0 To UBound(vArray)
    vResult(lInd) = vArray(j)
    If lInd = UBound(vArray) Then
        lRow = lRow + 1
        Range("J" & lRow).Resize(1, UBound(vArray) - LBound(vArray) + 1).Value = vResult
    Else
        perm1 vArray, vResult, lInd + 1, lRow
    End If
 Next j
End Sub

But it deals with just 1 single dimension array.

Honestly I am not very familiar with recursive programming and adjusting the code for these new constraints is a bit above my head.

Any help would be greatly appreciated. Thanks.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
note sure this is what you need.

when I ran it it stopped after 65,000 cominations

Code:
Sub do_it()
Application.ScreenUpdating = False
wr = 1

For e = 3 To Cells(Rows.Count, "E").End(xlUp).Row
For f = 3 To Cells(Rows.Count, "F").End(xlUp).Row
For g = 3 To Cells(Rows.Count, "G").End(xlUp).Row
For h = 3 To Cells(Rows.Count, "H").End(xlUp).Row
For i = 3 To Cells(Rows.Count, "I").End(xlUp).Row
For j = 3 To Cells(Rows.Count, "J").End(xlUp).Row

data = Cells(e, "E") & ", " & Cells(f, "F") & ", " & Cells(g, "G") & ", " & Cells(h, "H") & ", " & Cells(i, "I") & ", " & Cells(j, "J")


Cells(wr, "A") = data
wr = wr + 1
Next
Next
Next
Next
Next
Next

Application.ScreenUpdating = True
End Sub

Ross
 
Upvote 0
Thanks for the reply, but this is not quite what I am looking for.

The numbers at the top indicate the # of people that need to be in each listing.

So, for the output that you have going into column A, there are only 5 names in each cell.

There should be 9.

1 from P1
2 from P2
3 from P3
1 from P4
1 from P5 and
1 from P6

So the first example was

Andy, Doug, Eddie, Allen, Calvin, Eric, Greg, Graham, Bill

and the second is just going on to the next person in P6, which would be Sanchez.

Then once we've gone through all the names in P6, it would move to the next person in P5, Mason, and revert to the 1st name in P6, Bill.

Let me know if this makes sense,

Thanks again for looking at this.
 
Upvote 0
What are you trying to accomplish with this? I believe I understand what you're asking, and I can design a macro to accomplish it, but I believe the results will be fairly useless. According to my calculations, the example above will generate 62,548,200 different combinations. That's roughly 60 times the maximum number of rows you can put on an Excel sheet. Assuming I came up with a way to store/print all the combinations, how will you sift through them all to find the ones you want?
 
Upvote 0

Forum statistics

Threads
1,221,449
Messages
6,159,930
Members
451,604
Latest member
SWahl

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