Hi Narayan
Its me again. My friend who runs a Lottery Syndicate asked me it I could give him a formula or a macro to give him all the possible combinations of the numbers from 1-49 which as you are aware runs into millions (13983816 to be precise) what he is going to do with it I have no idea but it will be on his computer not mine so I am not bothered.Anyway I have made this macro but I have gone wrong somewhere when I ask it to give me the numbers in the combo say sixty(60) it just gives all the combinations that add up to 60 i.e 1,2,3,4,5,45. If I ask it for 13983816 it just says no combinations. So I wondered wether you could run your expert eye over it to see were I have gone wrong.
[pre]
[/pre]
Many Thanks
Mike
Its me again. My friend who runs a Lottery Syndicate asked me it I could give him a formula or a macro to give him all the possible combinations of the numbers from 1-49 which as you are aware runs into millions (13983816 to be precise) what he is going to do with it I have no idea but it will be on his computer not mine so I am not bothered.Anyway I have made this macro but I have gone wrong somewhere when I ask it to give me the numbers in the combo say sixty(60) it just gives all the combinations that add up to 60 i.e 1,2,3,4,5,45. If I ask it for 13983816 it just says no combinations. So I wondered wether you could run your expert eye over it to see were I have gone wrong.
[pre]
Code:
Sub Combos()
' Find all the combinations of six integer numbers, each in the range 1..49,
' that sum to the value of a desired total. A combination may not have two
' numbers that are the same. Results are written to columns A..F of the
' active worksheet.
Dim Total As Integer
Dim N1 As Integer
Dim N2 As Integer, Sum2 As Integer
Dim N3 As Integer, Sum3 As Integer
Dim N4 As Integer, Sum4 As Integer
Dim N5 As Integer, Sum5 As Integer
Dim N6 As Integer
Dim iRow As Long
Total = CInt(InputBox("Enter desired total", "Combos", 22))
If Total = 0 Then Exit Sub
iRow = 0
For N6 = (Total + 15) 6 To 49
For N5 = 5 To N6 - 1
Sum5 = N5 + N6
For N4 = 4 To N5 - 1
Sum4 = Sum5 + N4
For N3 = 3 To N4 - 1
Sum3 = Sum4 + N3
For N2 = 2 To N3 - 1
Sum2 = Sum3 + N2
N1 = Total - Sum2
If N1 < 1 Then Exit For
If N1 < N2 Then
iRow = iRow + 1
Cells(iRow, 1) = N1
Cells(iRow, 2) = N2
Cells(iRow, 3) = N3
Cells(iRow, 4) = N4
Cells(iRow, 5) = N5
Cells(iRow, 6) = N6
End If
Next N2
Next N3
Next N4
Next N5
Next N6
MsgBox iRow & " combinations found.", vbInformation, "Combos"
End Sub
Many Thanks
Mike