Option Explicit
Sub RunLottoSpecial()
Call LottoSpecial(45, 134)
End Sub
Sub LottoSpecial(Num As Long, TargetVal As Long)
Dim a As Integer, _
b As Integer, _
c As Integer, _
d As Integer, _
e As Integer, _
f As Integer
Dim Counter As Long, _
NumCols As Long, _
i As Long
Dim arrResults
Application.ScreenUpdating = False
For a = 1 To Num - 5
For b = a + 1 To Num - 4
For c = b + 1 To Num - 3
For d = c + 1 To Num - 2
For e = d + 1 To Num - 1
For f = e + 1 To Num
If a + b + c + d + e + f = TargetVal Then
Application.StatusBar = Counter
Counter = Counter + 1
With ActiveSheet
If Counter Mod 65536 = 0 Then
.Cells(65536, i + 1) = a & "," & b & "," & c & "," & d & "," & e & "," & f
i = i + 1
Else
.Cells(Counter Mod 65536, i + 1) = a & "," & b & "," & c & "," & d & "," & e & "," & f
End If
End With
End If
Next f
Next e
Next d
Next c
Next b
Next a
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
Sub MultipleLottoSums()
Dim i As Long, Wks As Worksheet
With ThisWorkbook
For i = 60 To 120
Set Wks = .Worksheets.Add
With Wks
.Activate
.Name = "Sum_to_" & i
Call LottoSpecial(45, i)
End With
Next i
End With
End Sub
Sub MultipleLottoSums()
Dim i As Long
On Error GoTo Er
Make_TextFile
Open ActiveWorkbook.Path & "Report.txt" For Output As #1
For i = 60 To 120
Application.StatusBar = "Finding combination ....Total sum = " & i
Call LottoSpecial(45, i)
Next i
Application.StatusBar = ""
Close #1
MsgBox "Check text file .... " & ActiveWorkbook.Path & "Report.txt"
Exit Sub
Er:
MsgBox "Error"
End Sub
Sub LottoSpecial(Num As Long, TargetVal As Long)
Dim a As Integer, _
b As Integer, _
c As Integer, _
d As Integer, _
e As Integer, _
f As Integer
Dim Counter As Long, _
NumCols As Long, _
i As Long
Dim arrResults
Dim varTemp As String
For a = 1 To Num - 5
For b = a + 1 To Num - 4
For c = b + 1 To Num - 3
For d = c + 1 To Num - 2
For e = d + 1 To Num - 1
For f = e + 1 To Num
If a + b + c + d + e + f = TargetVal Then
varTemp = a & "," & b & "," & c & "," & d & "," & e & "," & f
Write #1, varTemp
varTemp = ""
End If
Next f
Next e
Next d
Next c
Next b
Next a
End Sub
Sub Make_TextFile()
Open ActiveWorkbook.Path & "Report.txt" For Output As #1
Close #1
End Sub
Sub test()
Dim i As Integer, ii As Integer, iii As Integer, iv As Integer
Dim v As Integer, vi As Integer, n As Long, t As Integer, b()
t = 1 : n = 1
ReDim b(1 To Rows.Count, 1 To 1)
For i = 1 To 45 - 5
For ii = i + 1 To 45 - 4
For iii = ii + 1 To 45 - 3
For iv = iii + 1 To 45 - 2
For v = iv + 1 To 45 - 1
For vi = v + 1 To 45
If i + ii + iii + iv + v + vi = 134 Then
b(n,1) = i & "," & ii & "," & iii & "," & iv & "," & v & "," vi
End If
n = n + 1
If n > Rows.Count Then
Cells(1,t).Resize(Rows.Count).Value = b
n = 1 : t = t + 1
Redim b(1 To Rows.Count, 1 To 1)
End If
Next vi, v, iv, iii, ii, i
Cells(1,t).Resize(n).Value = b
End Sub