VBA generate 5_50 combinations from (column A) list.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I want a VBA that can generate 5_50 lottery combinations from list is in the column A from cell A6 to down. In this example there are 13 numbers which will generate total =COMBINAT(13;5)=1287 combination I want to list all them in Columns C:G

Excel Questions.xlsm
ABCDEFGH
1
2
3
4Total Combi
5Numbers1.287n1n2n3n4n5
61124612
72124615
84124616
96124626
1012124628
1115124631
1216124637
1326124641
1428124643
15311241215
16371241216
17411241226
18431241228
191241231
201241237
211241241
221241243
23
24
25
26
27
Sheet5
Cell Formulas
RangeFormula
B5B5=COMBIN(13,5)


Regards,
Moti
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try:

VBA Code:
Sub Test()

    Dim lResults() As Long, lOutput() As Long, No() As Long, i As Long, j As Long
    Const N = 13
    Const k = 5
    
    ReDim No(1 To N)
    lResults = GetCombinations(N, k)
    ReDim lOutput(1 To UBound(lResults), 1 To UBound(lResults, 2))
    
    For i = 1 To N
        No(i) = Range("A" & i + 5).Value
    Next i
    For i = 1 To UBound(lResults)
        For j = 1 To UBound(lResults, 2)
            lOutput(i, j) = No(lResults(i, j))
        Next j
    Next i
        
    Range("C6").Resize(UBound(lResults), UBound(lResults, 2)).Value = lOutput

End Sub
Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
    
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
    
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
    
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
    
    GetCombinations = lOutput
    
End Function
 
Upvote 1
Solution
Try:

VBA Code:
Sub Test()

    Dim lResults() As Long, lOutput() As Long, No() As Long, i As Long, j As Long
    Const N = 13
    Const k = 5
 
    ReDim No(1 To N)
    lResults = GetCombinations(N, k)
    ReDim lOutput(1 To UBound(lResults), 1 To UBound(lResults, 2))
 
    For i = 1 To N
        No(i) = Range("A" & i + 5).Value
    Next i
    For i = 1 To UBound(lResults)
        For j = 1 To UBound(lResults, 2)
            lOutput(i, j) = No(lResults(i, j))
        Next j
    Next i
   
    Range("C6").Resize(UBound(lResults), UBound(lResults, 2)).Value = lOutput

End Sub
Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
 
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
 
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
 
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
 
    GetCombinations = lOutput
 
End Function
StephenCrump, I am grateful, thank you so much, for giving me a perfect solution. I loved and it worked like magic. 👌

Have a great time and Good Luck.

Kind regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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