Combination of 6 numbers from a range of cells, which may contain up to 19 random numbers - no duplicates

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi There,

I have read a few of the posting about lotto number generations, I have a slightly different twist on this subject.

I am evaluating a series of number based on history and some calculations, from that I will select my own hot and cold numbers into a list.
What I am trying to do is generate combinations of 6 numbers based on my list of numbers.

I may select a list of 15 numbers, or I may select a list of 20 numbers this is all dependent on what I select.

So if i choose this range of numbers how can I generate combinations of 6 from this list, without duplicate combinations of 6.

1​
3​
5​
8​
11​
14​
21​
22​
24​
27​
29​
31​
32​
35​
37​
13​
17​
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
With a total of 17 different numbers, drawn 6 at a time, you are looking at 12,376 different combinations of 6 numbers.

If that is what you are looking for, we need to know the address range of where you are storing your numbers to make combinations from. For example are they stored in the A column starting at row 1?

Where would you like the results posted to? Same sheet, different sheet, etc.
 
Last edited:
Upvote 0
combination = 17^6 ~ 24 milions cases

Check your math @bebo021999. ;) That would be 17*17*17*17*17*17

We are dealing with different combinations here where the same number can not be drawn each and every time of the 6 drawn combination. ;)

All 6 # combinations have to be unique also. IE. 1,2,3,4,5,6 would be the the same as 1,2,3,6,5,4 so therefor they would be equivalent and only one of those would be counted.
 
Upvote 0
17^6 = 17*17*17*17*17*17 ~ 24 milion
For instant list of number = 1-17
it could be 1-1-1-1-1-1 (same number)
until 17-17-17-17-17-17
OP said "without duplicate combinations", not "duplicate number"
 
Upvote 0
Umm, How exactly ,... aww never mind, I have said enough.
 
Upvote 0
Anywho @jag108 , the following script assumes your numbers are in the A column and you want the results posted in the B column:

VBA Code:
Sub ListThemAllViaArray()
'
    Dim ArraySlotCount                  As Long
    Dim Ball_1                          As Long, Ball_2     As Long, Ball_3 As Long, Ball_4 As Long, Ball_5 As Long, Ball_6 As Long
    Dim CombinationCounter              As Long
    Dim LastRowOfNumbers                As Long
    Dim MaxRows                         As Long, ThisRow  As Long
    Dim MaxNumberOfBalls                As Long
    Dim NumberOfBallsToBeDrawnEachDraw  As Long
    Dim StartRowOfNumbers               As Long
    Dim TotalExpectedCominations        As Long
    Dim ResultsStartColumn              As Long
    Dim CombinationsArray(1 To 65536)   As Variant
    Dim MyNumbersArray                  As Variant
    Dim wsSource                        As Worksheet
'
    Set wsSource = Sheets("Sheet1")                                                                         ' <--- Set this to sheet that contains your numbers
    StartRowOfNumbers = 1                                                                                   ' <--- Set this to the start row of your numbers
    NumberOfBallsToBeDrawnEachDraw = 6                                                                      ' <--- Set this to the NumberOfBallsToBeDrawnEachDraw
    ResultsStartColumn = 2                                                                                  ' <--- Set this to the column # to display results in
'
    LastRowOfNumbers = wsSource.Range("A" & Rows.Count).End(xlUp).Row                                       ' Get last used row of numbers
'
    MyNumbersArray = wsSource.Range("A" & StartRowOfNumbers & ":A" & LastRowOfNumbers)                      ' Load numbers into 2D 1 based array RC
'
''    MaxNumberOfBalls = 44                                                                                   ' Total number of balls
    MaxNumberOfBalls = LastRowOfNumbers - StartRowOfNumbers + 1                                             ' Total number of balls
'
    ArraySlotCount = 0                                                                                      ' Initialize ArraySlotCount
    CombinationCounter = 1                                                                                  ' Initialize CombinationCounter
    MaxRows = 65536                                                                                         ' Set to maximum number of slots in Array
    ThisRow = 0                                                                                             ' Initialize row counter
    TotalExpectedCominations = Application.Combin(MaxNumberOfBalls, NumberOfBallsToBeDrawnEachDraw)         ' Set expected # of total combinations
'
    Application.ScreenUpdating = False                                                                      ' Turn Screen Updating off
'
    For Ball_1 = 1 To MaxNumberOfBalls - 5                                                                  ' Establish loop for 1st ball
        For Ball_2 = (Ball_1 + 1) To MaxNumberOfBalls - 4                                                   '   Establish loop for 2nd ball
            For Ball_3 = (Ball_2 + 1) To MaxNumberOfBalls - 3                                               '       Establish loop for 3rd ball
                For Ball_4 = (Ball_3 + 1) To MaxNumberOfBalls - 2                                           '           Establish loop for 4th ball
                    For Ball_5 = (Ball_4 + 1) To MaxNumberOfBalls - 1                                       '               Establish loop for 5th ball
                        For Ball_6 = (Ball_5 + 1) To MaxNumberOfBalls                                       '                   Establish loop for 6th ball
'
                            ArraySlotCount = ArraySlotCount + 1                                             '                       Increment ArraySlotCount
'
'                           Save combination into array
                            CombinationsArray(ArraySlotCount) = MyNumbersArray(Ball_1, 1) & "-" & MyNumbersArray(Ball_2, 1) & "-" & MyNumbersArray(Ball_3, 1) & "-" & MyNumbersArray(Ball_4, 1) & "-" & MyNumbersArray(Ball_5, 1) & "-" & MyNumbersArray(Ball_6, 1)
                            CombinationCounter = CombinationCounter + 1                                     '                       Increment CombinationCounter
'
                            If CombinationCounter Mod 550000 = 0 Then                                       '                       If CombinationCounter = 550k then ...
'                               Update StatusBar about every 10 seconds
                                Application.StatusBar = "Result " & CombinationCounter & " on way to " & TotalExpectedCominations
'
                                DoEvents                                                                    '                           DoEvents
                            End If
'
                            ThisRow = ThisRow + 1                                                           '                       Increment row counter
'
                            If ThisRow = MaxRows Then                                                       '                       If row count=array max slots
'                               Dump contents of CombinationsArray to the screen
                                wsSource.Range(Cells(1, ResultsStartColumn), wsSource.Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)
'
                                Erase CombinationsArray                                                     '                           Erase contents of array
                                ArraySlotCount = 0                                                          '                           Reset ArraySlotCount
                                ThisRow = 0                                                                 '                           Reset row counter
                                ResultsStartColumn = ResultsStartColumn + 1                                 '                           Increment column counter
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
'
    Range(Cells(1, ResultsStartColumn), Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)  ' Dump contents of last array to screen
'
    Columns.AutoFit                                                                                         ' Resize all columns to fit the data within them
    Application.ScreenUpdating = True                                                                       ' Turn Screen Updating back on
End Sub
 
Upvote 0
With a total of 17 different numbers, drawn 6 at a time, you are looking at 12,376 different combinations of 6 numbers.

If that is what you are looking for, we need to know the address range of where you are storing your numbers to make combinations from. For example are they stored in the A column starting at row 1?

Where would you like the results posted to? Same sheet, different sheet, etc.
Same sheet, I can change if things become to unwieldly, thank you.
 
Upvote 0
Anywho @jag108 , the following script assumes your numbers are in the A column and you want the results posted in the B column:

VBA Code:
Sub ListThemAllViaArray()
'
    Dim ArraySlotCount                  As Long
    Dim Ball_1                          As Long, Ball_2     As Long, Ball_3 As Long, Ball_4 As Long, Ball_5 As Long, Ball_6 As Long
    Dim CombinationCounter              As Long
    Dim LastRowOfNumbers                As Long
    Dim MaxRows                         As Long, ThisRow  As Long
    Dim MaxNumberOfBalls                As Long
    Dim NumberOfBallsToBeDrawnEachDraw  As Long
    Dim StartRowOfNumbers               As Long
    Dim TotalExpectedCominations        As Long
    Dim ResultsStartColumn              As Long
    Dim CombinationsArray(1 To 65536)   As Variant
    Dim MyNumbersArray                  As Variant
    Dim wsSource                        As Worksheet
'
    Set wsSource = Sheets("Sheet1")                                                                         ' <--- Set this to sheet that contains your numbers
    StartRowOfNumbers = 1                                                                                   ' <--- Set this to the start row of your numbers
    NumberOfBallsToBeDrawnEachDraw = 6                                                                      ' <--- Set this to the NumberOfBallsToBeDrawnEachDraw
    ResultsStartColumn = 2                                                                                  ' <--- Set this to the column # to display results in
'
    LastRowOfNumbers = wsSource.Range("A" & Rows.Count).End(xlUp).Row                                       ' Get last used row of numbers
'
    MyNumbersArray = wsSource.Range("A" & StartRowOfNumbers & ":A" & LastRowOfNumbers)                      ' Load numbers into 2D 1 based array RC
'
''    MaxNumberOfBalls = 44                                                                                   ' Total number of balls
    MaxNumberOfBalls = LastRowOfNumbers - StartRowOfNumbers + 1                                             ' Total number of balls
'
    ArraySlotCount = 0                                                                                      ' Initialize ArraySlotCount
    CombinationCounter = 1                                                                                  ' Initialize CombinationCounter
    MaxRows = 65536                                                                                         ' Set to maximum number of slots in Array
    ThisRow = 0                                                                                             ' Initialize row counter
    TotalExpectedCominations = Application.Combin(MaxNumberOfBalls, NumberOfBallsToBeDrawnEachDraw)         ' Set expected # of total combinations
'
    Application.ScreenUpdating = False                                                                      ' Turn Screen Updating off
'
    For Ball_1 = 1 To MaxNumberOfBalls - 5                                                                  ' Establish loop for 1st ball
        For Ball_2 = (Ball_1 + 1) To MaxNumberOfBalls - 4                                                   '   Establish loop for 2nd ball
            For Ball_3 = (Ball_2 + 1) To MaxNumberOfBalls - 3                                               '       Establish loop for 3rd ball
                For Ball_4 = (Ball_3 + 1) To MaxNumberOfBalls - 2                                           '           Establish loop for 4th ball
                    For Ball_5 = (Ball_4 + 1) To MaxNumberOfBalls - 1                                       '               Establish loop for 5th ball
                        For Ball_6 = (Ball_5 + 1) To MaxNumberOfBalls                                       '                   Establish loop for 6th ball
'
                            ArraySlotCount = ArraySlotCount + 1                                             '                       Increment ArraySlotCount
'
'                           Save combination into array
                            CombinationsArray(ArraySlotCount) = MyNumbersArray(Ball_1, 1) & "-" & MyNumbersArray(Ball_2, 1) & "-" & MyNumbersArray(Ball_3, 1) & "-" & MyNumbersArray(Ball_4, 1) & "-" & MyNumbersArray(Ball_5, 1) & "-" & MyNumbersArray(Ball_6, 1)
                            CombinationCounter = CombinationCounter + 1                                     '                       Increment CombinationCounter
'
                            If CombinationCounter Mod 550000 = 0 Then                                       '                       If CombinationCounter = 550k then ...
'                               Update StatusBar about every 10 seconds
                                Application.StatusBar = "Result " & CombinationCounter & " on way to " & TotalExpectedCominations
'
                                DoEvents                                                                    '                           DoEvents
                            End If
'
                            ThisRow = ThisRow + 1                                                           '                       Increment row counter
'
                            If ThisRow = MaxRows Then                                                       '                       If row count=array max slots
'                               Dump contents of CombinationsArray to the screen
                                wsSource.Range(Cells(1, ResultsStartColumn), wsSource.Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)
'
                                Erase CombinationsArray                                                     '                           Erase contents of array
                                ArraySlotCount = 0                                                          '                           Reset ArraySlotCount
                                ThisRow = 0                                                                 '                           Reset row counter
                                ResultsStartColumn = ResultsStartColumn + 1                                 '                           Increment column counter
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
'
    Range(Cells(1, ResultsStartColumn), Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)  ' Dump contents of last array to screen
'
    Columns.AutoFit                                                                                         ' Resize all columns to fit the data within them
    Application.ScreenUpdating = True                                                                       ' Turn Screen Updating back on
End Sub
Anywho @jag108 , the following script assumes your numbers are in the A column and you want the results posted in the B column:

VBA Code:
Sub ListThemAllViaArray()
'
    Dim ArraySlotCount                  As Long
    Dim Ball_1                          As Long, Ball_2     As Long, Ball_3 As Long, Ball_4 As Long, Ball_5 As Long, Ball_6 As Long
    Dim CombinationCounter              As Long
    Dim LastRowOfNumbers                As Long
    Dim MaxRows                         As Long, ThisRow  As Long
    Dim MaxNumberOfBalls                As Long
    Dim NumberOfBallsToBeDrawnEachDraw  As Long
    Dim StartRowOfNumbers               As Long
    Dim TotalExpectedCominations        As Long
    Dim ResultsStartColumn              As Long
    Dim CombinationsArray(1 To 65536)   As Variant
    Dim MyNumbersArray                  As Variant
    Dim wsSource                        As Worksheet
'
    Set wsSource = Sheets("Sheet1")                                                                         ' <--- Set this to sheet that contains your numbers
    StartRowOfNumbers = 1                                                                                   ' <--- Set this to the start row of your numbers
    NumberOfBallsToBeDrawnEachDraw = 6                                                                      ' <--- Set this to the NumberOfBallsToBeDrawnEachDraw
    ResultsStartColumn = 2                                                                                  ' <--- Set this to the column # to display results in
'
    LastRowOfNumbers = wsSource.Range("A" & Rows.Count).End(xlUp).Row                                       ' Get last used row of numbers
'
    MyNumbersArray = wsSource.Range("A" & StartRowOfNumbers & ":A" & LastRowOfNumbers)                      ' Load numbers into 2D 1 based array RC
'
''    MaxNumberOfBalls = 44                                                                                   ' Total number of balls
    MaxNumberOfBalls = LastRowOfNumbers - StartRowOfNumbers + 1                                             ' Total number of balls
'
    ArraySlotCount = 0                                                                                      ' Initialize ArraySlotCount
    CombinationCounter = 1                                                                                  ' Initialize CombinationCounter
    MaxRows = 65536                                                                                         ' Set to maximum number of slots in Array
    ThisRow = 0                                                                                             ' Initialize row counter
    TotalExpectedCominations = Application.Combin(MaxNumberOfBalls, NumberOfBallsToBeDrawnEachDraw)         ' Set expected # of total combinations
'
    Application.ScreenUpdating = False                                                                      ' Turn Screen Updating off
'
    For Ball_1 = 1 To MaxNumberOfBalls - 5                                                                  ' Establish loop for 1st ball
        For Ball_2 = (Ball_1 + 1) To MaxNumberOfBalls - 4                                                   '   Establish loop for 2nd ball
            For Ball_3 = (Ball_2 + 1) To MaxNumberOfBalls - 3                                               '       Establish loop for 3rd ball
                For Ball_4 = (Ball_3 + 1) To MaxNumberOfBalls - 2                                           '           Establish loop for 4th ball
                    For Ball_5 = (Ball_4 + 1) To MaxNumberOfBalls - 1                                       '               Establish loop for 5th ball
                        For Ball_6 = (Ball_5 + 1) To MaxNumberOfBalls                                       '                   Establish loop for 6th ball
'
                            ArraySlotCount = ArraySlotCount + 1                                             '                       Increment ArraySlotCount
'
'                           Save combination into array
                            CombinationsArray(ArraySlotCount) = MyNumbersArray(Ball_1, 1) & "-" & MyNumbersArray(Ball_2, 1) & "-" & MyNumbersArray(Ball_3, 1) & "-" & MyNumbersArray(Ball_4, 1) & "-" & MyNumbersArray(Ball_5, 1) & "-" & MyNumbersArray(Ball_6, 1)
                            CombinationCounter = CombinationCounter + 1                                     '                       Increment CombinationCounter
'
                            If CombinationCounter Mod 550000 = 0 Then                                       '                       If CombinationCounter = 550k then ...
'                               Update StatusBar about every 10 seconds
                                Application.StatusBar = "Result " & CombinationCounter & " on way to " & TotalExpectedCominations
'
                                DoEvents                                                                    '                           DoEvents
                            End If
'
                            ThisRow = ThisRow + 1                                                           '                       Increment row counter
'
                            If ThisRow = MaxRows Then                                                       '                       If row count=array max slots
'                               Dump contents of CombinationsArray to the screen
                                wsSource.Range(Cells(1, ResultsStartColumn), wsSource.Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)
'
                                Erase CombinationsArray                                                     '                           Erase contents of array
                                ArraySlotCount = 0                                                          '                           Reset ArraySlotCount
                                ThisRow = 0                                                                 '                           Reset row counter
                                ResultsStartColumn = ResultsStartColumn + 1                                 '                           Increment column counter
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
'
    Range(Cells(1, ResultsStartColumn), Cells(ThisRow, ResultsStartColumn)) = Application.Transpose(CombinationsArray)  ' Dump contents of last array to screen
'
    Columns.AutoFit                                                                                         ' Resize all columns to fit the data within them
    Application.ScreenUpdating = True                                                                       ' Turn Screen Updating back on
End Sub
Thank you much I appreciate your time and effort very much.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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