Hi,
To calculate all the combinations in a 52 number lottery then alter the
array dimension as you note to
Dim n(52)
and then put all 52 numbers in column A and be prepared for a long wait.
But remember this was written to specifically to only produce combinations
that add up to 138 as requested by the OP and more usually people would
want
to all combinations of a set of numbers.
The code below has been edited to produce all the combinations of a set of
numbers so if you want to (say) produce all the combinations of 10 numbers
put those numbers in column A and run the code, For example these numbers
in
column A but you can put as many as you like up to 52
1
2
3
45
47
48
49
52
Would produce the 28 combinations possible.
HTH Mike
Sub thelottery()
Application.ScreenUpdating = False
Count = 1
col = 2
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set myRange = Range("A1:A" & lastrow)
For Each c In myRange
numbers = numbers + 1
Next
Dim n(52)
For p = 1 To numbers
n(p) = Cells(p, 1).Value
Next
For i = 1 To numbers
For j = 1 To numbers
If j <= i Then GoTo 100
For k = 1 To numbers
If k <= j Then GoTo 200
For l = 1 To numbers
If l <= k Then GoTo 300
For m = 1 To numbers
If m <= l Then GoTo 400
For o = 1 To numbers
If o <= m Then GoTo 500
For x = 1 To numbers
If i = x Then firstno =
n(x)
Next
For x = 1 To numbers
If j = x Then secondno =
n(x)
Next
For x = 1 To numbers
If k = x Then thirdno =
n(x)
Next
For x = 1 To numbers
If l = x Then fourthno =
n(x)
Next
For x = 1 To numbers
If m = x Then fifthno =
n(x)
Next
For x = 1 To numbers
If o = x Then sixthno =
n(x)
Next
Cells(Count, col).Value = firstno & "," & secondno
&
"," & thirdno & "," & fourthno & "," & fifthno & "," & sixthno
If Count = 65536 Then
total = total + Count
Count = 1
col = col + 1
Else
Count = Count + 1
End If
500 Next
400 Next
300 Next
200 Next
100 Next
Next
Cells(1, 8).Value = (total + Count) - 1
Application.ScreenUpdating = True
End Sub