Using Excel 2010
Hello,
@johnnyL, I found below code which generate all combinations from 1st to last set of 5 combinations. For 5_50 lottery
For example 1st 1-2-3-4-5 and last 46-47-48-49-50 total 2118760 combinations.
Does it is possible to modify so I can select 1st number of choice for example number “1” so it give me only all combination with number “1”. If 2 then with 3 if 3 then with 3 if I select 46 then it give me only 1 combination 46-47-48-49-50.
Regards,
Moti
Hello,
@johnnyL, I found below code which generate all combinations from 1st to last set of 5 combinations. For 5_50 lottery
For example 1st 1-2-3-4-5 and last 46-47-48-49-50 total 2118760 combinations.
Does it is possible to modify so I can select 1st number of choice for example number “1” so it give me only all combination with number “1”. If 2 then with 3 if 3 then with 3 if I select 46 then it give me only 1 combination 46-47-48-49-50.
VBA Code:
Sub MegaMillionsAllCombinations_OneCellEach_SmallerArrays()
'
Dim StartTime As Double
StartTime = Timer
'
Dim ArrayRanges As Long, RangeCount As Long
Dim AmountOfNumbersChosen As Long, MaxAmountOfNumbers As Long
Dim DisplayColumn As Long, DisplayRow As Long
Dim MaxArrayRows As Long
Dim OutputColumn As Long
Dim StartOutputColumn As Long
Dim SourceRow As Long, OutputRow As Long
Dim OutputArray() As String, SourceArray() As Long
Dim HeaderArray As Variant
'
AmountOfNumbersChosen = 5 ' <--- Set this to the AmountOfNumbersChosen
MaxAmountOfNumbers = 50 ' <--- Set this to the MaxAmountOfNumbers
StartOutputColumn = 1 ' <--- Set this to the column to start displaying data to
'
' 1000000 is the maximum MaxArrayRows suggested
' It is recommended that the following value be set to value that is
' easily divisible by 1000000. Ie. 1M, 500k, 250k, 125k, 100k, 50k, 25k, 20k, 10k, 5k
MaxArrayRows = 1000000 ' <--- Set this to the MaxArrayRows in the OutputArray
'
ActiveSheet.UsedRange.ClearContents ' Clear any previous results from sheet
'
SourceArray = GetCombinations(MaxAmountOfNumbers, AmountOfNumbersChosen) ' Load SourceArray with all non repeating 5 out of 70 combinations
'
ArrayRanges = Application.WorksheetFunction.RoundUp(UBound(SourceArray, 1) _
/ 1000000, 0) ' Calculate # of loops needed to cycle through all combos
'
HeaderArray = Array("5 Ball Combinations", "Euromillones") ' Establish array of Headers to write to sheet
'
For RangeCount = 1 To ArrayRanges ' Loop through needed ranges of data
Cells(1, StartOutputColumn).Resize(1, UBound(HeaderArray) + 1) = HeaderArray ' Write the Header array to sheet for each range
StartOutputColumn = StartOutputColumn + UBound(HeaderArray) + 2 ' Increment the StartOutputColumn
Next ' Loop back
'
ActiveSheet.UsedRange.EntireColumn.AutoFit ' Set the width of the columns to be used
'
'---------------------------------------------------------------------------------------
'
ReDim OutputArray(1 To MaxArrayRows, 1 To 1) ' Set the # of rows & columns for the OutputArray
'
DisplayColumn = 1 ' Initialize DisplayColumn
DisplayRow = 2 ' Initialize DisplayRow
OutputRow = 1 ' Initialize the OutputRow
SourceRow = 0 ' Initialize SourceRow
OutputColumn = 1 ' Initialize the OutputColumn
'
For SourceRow = 1 To UBound(SourceArray, 1) ' Loop through all generated 5 ball combinations of 70 balls total
OutputArray(OutputRow, OutputColumn) = SourceArray(SourceRow, 1) & _
"-" & SourceArray(SourceRow, 2) & "-" & SourceArray(SourceRow, 3) & _
"-" & SourceArray(SourceRow, 4) & "-" & SourceArray(SourceRow, 5) ' Save combined numbers and delimeters to OutputArray
'
OutputRow = OutputRow + 1 ' Increment the OutputRow
'
If OutputRow > MaxArrayRows Then ' If we have copied 50k data rows to OutputArray then ...
OutputRow = 1 ' Reset OutputRow
'
Application.ScreenUpdating = False ' Turn ScreenUpdating off
Cells(DisplayRow, DisplayColumn).Resize(UBound(OutputArray, 1), _
UBound(OutputArray, 2)) = OutputArray ' Display results to sheet
Application.ScreenUpdating = True ' Turn ScreenUpdating back on
'
DoEvents ' Allow sheet to display current written data
'
ReDim OutputArray(1 To MaxArrayRows, 1 To 1) ' Set the # of rows & columns for the OutputArray
'
DisplayRow = DisplayRow + MaxArrayRows ' Increment DisplayRow
'
If Cells(Rows.Count, DisplayColumn).End(xlUp).Row > 1000000 Then ' If sheet column is full then ...
DisplayRow = 2 ' Reset DisplayRow
DisplayColumn = DisplayColumn + 3 ' Increment the DisplayColumn
End If
End If
Next ' Loop back
'
If OutputRow > 1 Then ' If there are more results to display then ...
Application.ScreenUpdating = False ' Turn ScreenUpdating off
Cells(DisplayRow, DisplayColumn).Resize(OutputRow - 1, _
UBound(OutputArray, 2)) = OutputArray ' Display remaining results to sheet
Application.ScreenUpdating = True ' Turn ScreenUpdating back on
End If
'
Debug.Print "Time to complete = " & Timer - StartTime & " seconds." ' Display time to complete to 'Immediate' window Ctrl+G in VBE
MsgBox "Time to complete = " & Timer - StartTime & " seconds." ' Display time to complete in a message box
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
Regards,
Moti