All combinations for a 6 / 45 lottery
The following macro will list all 8,145,060 combinations for a 6 / 45 lottery in the active sheet.
Option Explicit
Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
Dim N As Long, nMinA As Integer, nMaxF As Integer
Sub List_6x45Comb()
' Macro to list combinations in an Excel sheet
Sheets("List_Comb").Select
Range("A1").Select
Application.ScreenUpdating = False
N = 1
Selection.ColumnWidth = 18
ActiveCell.Value = "Comb."
' Change minimum value for A or maximum value for F
' to cover range of values to be covered
nMinA = 1
nMaxF = 45
' Start of loops for 6 variables (A-F)
For A = nMinA To nMaxF - 5
For B = A + 1 To nMaxF - 4
For C = B + 1 To nMaxF - 3
For D = C + 1 To nMaxF - 2
For E = D + 1 To nMaxF - 1
For F = E + 1 To nMaxF
' Verification of number of combinations posted
' and move to top of next column if 65,000
If N = 65001 Then
Selection.ColumnWidth = 18
N = 1
ActiveCell.Offset(-65000, 1).Select
Application.ScreenUpdating = True
Application.ScreenUpdating = False
ActiveCell.Value = "Comb."
End If
' Copying the combination in the form N1-N2-N3-N4-N5-N6 in a cell
ActiveCell.Offset(1, 0).Select
N = N + 1
ActiveCell.Value = Application.WorksheetFunction.Text(A, "00") & "-" _
& Application.WorksheetFunction.Text(B, "00") & "-" _
& Application.WorksheetFunction.Text(C, "00") & "-" _
& Application.WorksheetFunction.Text(D, "00") & "-" _
& Application.WorksheetFunction.Text(E, "00") & "-" _
& Application.WorksheetFunction.Text(F, "00")
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub