Hello everybody,
From A1 to A15 there are numbers ("1" to "15"). In B1, there is number "4". The following code will create combinations of these 15 numbers, in groups of 4. The first combination generated is "1, 2, 3, 4".
I need to make some changes so that the first combination generated is "1, 3, 5, 9" (and not "1, 2, 3, 4").
Any help will be greatly appreciated!
Regards,
John
From A1 to A15 there are numbers ("1" to "15"). In B1, there is number "4". The following code will create combinations of these 15 numbers, in groups of 4. The first combination generated is "1, 2, 3, 4".
Code:
Sub Combinations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Long, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Call CombinationsNP(vElements, p, vresult, 1, 1)
End Sub
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, iIndex As Integer)
Dim i As Integer
For i = iElement To UBound(vElements)
vresult(iIndex) = vElements(i)
If iIndex = p Then
Cells(Application.Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(, p) = vresult
Cells(Application.Rows.Count, 3).End(xlUp).Select
Else
Call CombinationsNP(vElements, p, vresult, i + 1, iIndex + 1)
Cells(Application.Rows.Count, 3).End(xlUp).Select
End If
Next i
End Sub
I need to make some changes so that the first combination generated is "1, 3, 5, 9" (and not "1, 2, 3, 4").
Any help will be greatly appreciated!
Regards,
John
Last edited: