Hello everyone, I have been using the below macro to calculate the number of permutations (output columns C onward) for a list (input column A) based on the number chosen (input column B). However, I now have a list that is further broken down by groupings. My goal is to be able to input this list into Column A with the corresponding group number in column B, input the number chosen in column c, and have the permutation output for each group listed all together in column C onwards. The permutations need to be calculated at the group level, meaning that items in group 1 should not be permuted against those in any other group. As always, any and all advice is appreciated. Thanks!
Below is the example output I am looking for.
<tbody>
</tbody>
Below is the example output I am looking for.
List of Permutations | |||
Name | Group | Employee A | Employee B |
Bob | 1 | Bob | Ginger |
Ginger | 1 | Ginger | Bob |
Tommy | 2 | Tommy | Dave |
Dave | 2 | Dave | Tommy |
Wendy | 3 | Wendy | Trisha |
Trisha | 3 | Wendy | Cindy |
Cindy | 3 | Wendy | Robert |
Robert | 3 | Trisha | Wendy |
Trisha | Cindy | ||
Trisha | Robert | ||
Cindy | Wendy | ||
Cindy | Trisha | ||
Cindy | Robert | ||
Robert | Wendy | ||
Robert | Trisha | ||
Robert | Cindy |
<tbody>
</tbody>
Code:
Sub Permutations()
Dim rRng As Range, p
Dim vElements, lRow As Long, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of values
p = Range("B1").Value ' How many are picked
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Application.ScreenUpdating = False
Call PermutationsNP(vElements, CInt(p), vresult, lRow, 1)
Application.ScreenUpdating = True
End Sub
Sub PermutationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iIndex As Integer)
Dim i As Long, j As Long, bSkip As Boolean
For i = 1 To UBound(vElements)
bSkip = False
For j = 1 To iIndex - 1
If vresult(j) = vElements(i) Then
bSkip = True
Exit For
End If
Next j
If Not bSkip Then
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
Range("C" & lRow).Resize(, p) = vresult
Else
Call PermutationsNP(vElements, p, vresult, lRow, iIndex + 1)
End If
End If
Next i
End Sub