Modify Array To Give All Results For UK Lottery

Toby123

New Member
Joined
Sep 6, 2023
Messages
20
Office Version
  1. 365
Platform
  1. MacOS
Thank you so much to Mr Excel for his post on populating a sheet with all the possible lottery results and to johnnyL for his fast array for generating the lottery results for a 44 ball lottery (copied below). I am looking to modify johnnyL's array for the UK lottery which is 59 balls, choose 6. As there are 45,057,474 possible results (!) the array is a great option. I managed to modify Mr Excels macro and it appeared to be working but crashed after two days! I also managed to get some results from johnnyL's code but it started returning errors in the cells. Thank you so much in advance for looking at this for me.



VBA Code:
Sub ListThemAllViaArray()
'
    Dim ArraySlotCount                  As Long
    Dim Ball_1                          As Long, Ball_2     As Long, Ball_3 As Long, Ball_4 As Long, Ball_5 As Long, Ball_6 As Long
    Dim CombinationCounter              As Long
    Dim MaxRows                         As Long, ThisRow  As Long
    Dim MaxWhiteBallValue               As Long
    Dim TotalExpectedCominations        As Long
    Dim ThisColumn                      As Long
    Dim CombinationsArray(1 To 65536)   As Variant
'
    MaxWhiteBallValue = 44                                                                                  ' <--- Set to highest value of white ball
'
    ArraySlotCount = 0                                                                                      ' Initialize ArraySlotCount
    CombinationCounter = 1                                                                                  ' Initialize CombinationCounter
    MaxRows = 65536                                                                                         ' Set to maximum number of slots in Array
    ThisColumn = 1                                                                                          ' Initialize 1st column to display results in
    ThisRow = 0                                                                                             ' Initialize row counter
    TotalExpectedCominations = 7059052                                                                      ' Set expected # of total combinations
'
      Application.ScreenUpdating = False                                                                    ' Turn Screen Updating off
'
    For Ball_1 = 1 To MaxWhiteBallValue - 5                                                                 ' Establish loop for 1st ball
        For Ball_2 = (Ball_1 + 1) To MaxWhiteBallValue - 4                                                  '   Establish loop for 2nd ball
            For Ball_3 = (Ball_2 + 1) To MaxWhiteBallValue - 3                                              '       Establish loop for 3rd ball
                For Ball_4 = (Ball_3 + 1) To MaxWhiteBallValue - 2                                          '           Establish loop for 4th ball
                    For Ball_5 = (Ball_4 + 1) To MaxWhiteBallValue - 1                                      '               Establish loop for 5th ball
                        For Ball_6 = (Ball_5 + 1) To MaxWhiteBallValue                                      '                   Establish loop for 6th ball
'
                            ArraySlotCount = ArraySlotCount + 1                                             '                       Increment ArraySlotCount
'
'                           Save combination into array
                            CombinationsArray(ArraySlotCount) = Ball_1 & "-" & Ball_2 & "-" & Ball_3 & "-" & Ball_4 & "-" & Ball_5 & "-" & Ball_6
                            CombinationCounter = CombinationCounter + 1                                     '                       Increment CombinationCounter
'
                            If CombinationCounter Mod 550000 = 0 Then                                       '                       If CombinationCounter = 550k then ...
'                               Update StatusBar about every 10 seconds
                                Application.StatusBar = "Result " & CombinationCounter & " on way to " & TotalExpectedCominations
'
                                DoEvents                                                                    '                           DoEvents
                            End If
'
                            ThisRow = ThisRow + 1                                                           '                       Increment row counter
'
                            If ThisRow = MaxRows Then                                                       '                       If row count=array max slots
'                               Dump contents of CombinationsArray to the screen
                                Range(Cells(1, ThisColumn), Cells(ThisRow, ThisColumn)) = Application.Transpose(CombinationsArray)
'
                                Erase CombinationsArray                                                     '                           Erase contents of array
                                ArraySlotCount = 0                                                          '                           Reset ArraySlotCount
                                ThisRow = 0                                                                 '                           Reset row counter
                                ThisColumn = ThisColumn + 1                                                 '                           Increment column counter
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
'
    Range(Cells(1, ThisColumn), Cells(ThisRow, ThisColumn)) = Application.Transpose(CombinationsArray)      ' Dump contents of last array to the screen
    Columns.AutoFit                                                                                         ' Resize all columns to fit the data within them
'
    Application.ScreenUpdating = True                                                                       ' Turn Screen Updating back on
End Sub
 
Last edited by a moderator:
The most likely cause of that is the Transpose has an upper limit of 65,536 at which point it start filling the array from scratch again.

Try changing these 4 lines to what I have below:

Rich (BB code):
    ' Change array from 1 to 2 dimensional
    Dim CombinationsArray(1 To 550000, 1 To 1)   As Variant
    MaxRows = 550000
                            ' Add 2nd Dimension eg column = 1
                            CombinationsArray(ArraySlotCount, 1) = Ball_1 & "-" & Ball_2 & "-" & Ball_3 & "-" & Ball_4 & "-" & Ball_5 & "-" & Ball_6
                               ' Array is now 2 dimensional and no longer needs the Transpose
                                Range(Cells(1, ThisColumn), Cells(ThisRow, ThisColumn)) = CombinationsArray
Thank you for this, I made the changes and ran the code and it still stops but without giving any errors in the cells this time. I have attached a screen capture of the point at which it stops. I think I might try using the replace function or something similar to create the rest of the cells from the ones I have.
 

Attachments

  • Screenshot 2023-09-11 at 12.11.11.png
    Screenshot 2023-09-11 at 12.11.11.png
    17.2 KB · Views: 12
Upvote 0
Thank you for this, I made the changes and ran the code and it still stops but without giving any errors in the cells this time. I have attached a screen capture of the point at which it stops. I think I might try using the replace function or something similar to create the rest of the cells from the ones I have.
I have just realised that isn't going to work. I was hoping to replace the first number in each cell but it's actually far more complicated than that. Will try and run the code somewhere else.
 
Upvote 0
I tried running both versions of the code (with and without Alex's helpful idea above) on a powerful remote Vagon PC, so Windows machine this time, and it is still stopping at the same point shown in the attached image. Is anyone feeling generous enough to try running the code for me and seeing what they get? Any chance of forwarding me the results if it works? I will be eternally grateful!
 

Attachments

  • Screenshot 2023-09-11 at 12.11.11.png
    Screenshot 2023-09-11 at 12.11.11.png
    17.2 KB · Views: 9
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Thank you; would you mind elaborating, not sure what you mean here!
 
Upvote 0
Thank you @I_know_nuffin; would you mind elaborating, not sure what you mean here! Thank you also Alex, when I run it on two different machines it is stopping at the point I mentioned above. Is the code you posted what you ran? If so I will try it again with that. Thank you all for your patience with this!
 
Upvote 0
Alex, when I run it on two different machines it is stopping at the point I mentioned above. Is the code you posted what you ran? If so I will try it again with that.
Yes that is the code I ran and it worked fine all be it very slow on my machine.
I think it runs quicker if you comment out the Do Events line, so give that a try.

With the Do Events commented out I have had it run in just under 7 mins. My previous run times have been as long as 15 mins.
 
Upvote 0
Yes that is the code I ran and it worked fine all be it very slow on my machine.
I think it runs quicker if you comment out the Do Events line, so give that a try.

With the Do Events commented out I have had it run in just under 7 mins. My previous run times have been as long as 15 mins.
It worked! Thank you, thank you, thank you! Not sure what was different, but when I copy and pasted your code it ran this time. Probably took about 14 minutes.
 
Upvote 0
Thanks for letting us know. All the credit goes to @johnnyL.

PS: If you haven't already done so, try commenting out the Do Events line and see if that doesn't reduce the 14 mins down. It did for me.
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top