I found this code online and I modified it to get me somewhat close to what I'm looking for. I have a list of 10 items and I'm looking to print every combination of 6 items (order is irrelevant but they cannot be duplicated). Right now it prints 6 to start and then eventually prints groups of 7,8,9 until it overflows. How do I get it to stop at 6 and not overflow? Thanks I appreciate your help!
Code:
' Set in A1, down. Result in C1, down and across. Clears C:Z.
Sub PowerSet()
Dim vElements As Variant, vresult As Variant
Dim lRow As Long, i As Long
vElements = Application.Transpose(Range("A1", Range("A1").End(xlDown)))
Columns("C:Z").Clear
lRow = 6
For i = 6 To UBound(vElements)
ReDim vresult(1 To i)
Call CombinationsNP(vElements, i, vresult, lRow, 1, 1)
Next i
End Sub
Sub CombinationsNP(vElements As Variant, p As Long, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
Dim i As Long
For i = iElement To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
lRow = lRow + 1
Range("C" & lRow).Resize(, p) = vresult
Else
Call CombinationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
End If
Next i
End Sub