Please modify the code for 5_50 lottery with specifics odd even conditions.

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

@johnnyL, I found your code which work perfect with my version 2000 as it is designed. Please does it is possible could be adapted for the lottery Euromillones 5_50 “5 balls out of 50”

Also I need bit more specific Odd/Even condition by their “5 positions” to generate the combinations. Conditions simple sheet with 3 examples is attached below.

Kishan Index.xlsx
ABCDEFGH
1
2Position-1Position-2Position-3Position-4Position-5
3Example1ODDODDODDODDODD
4Out Put13579
5
6Position-1Position-2Position-3Position-4Position-5
7Example2EVENODDODDEVENEVEN
8Out Put29154850
9
10Position-1Position-2Position-3Position-4Position-5
11Example3EVENEVENEVENODDODD
12Out Put 212223949
13
Hoja15


@johnnyL, code from below link.
Excel List All Lottery Combinations - 2441
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

Thank you in advance

Regards,
Kishan
 
Hi johnnyL, I need your help please guide me…

1-”Sub Generate_O_And_E_Permutations()” this worked fine created permutations in the “Hoja2”

2-“Private Sub Worksheet_SelectionChange(ByVal Target As Range)”this code I paste in “Hoja3” where data is located in:

3-“Sub List5of50ByOddAndEvenDesignation(ByVal OddEvenString As String)” pasted in the regular module…But I cannot run it.

Please can you tell me what I am doing wrong?

Thank you

Regards,
Kishan
Hi johnnyL, I am sorry this is my first time I seen code finally worked selecting just any permutation nº from the list P6:Q37 it is just amazing operational for me. I like it.

6 numbers-O/E permutations generate 53130 as you said it is perfect, 6 numbers-O/E permutations generate 80730 and 20-numbers O/E permutations generate 65780. This all I checked with version 2010 give me results okay. Total 32 permutations generate 2118760.
johnnyL-Kishan.xls
PQRSTUVW
110OEOOE
2
3Position-1Position-2Position-3Position-4Position-5
4Total UniqueUniqueUniqueUniqueUniqueSum
5PatternOdd/EvenOdd/EvenOdd/EvenOdd/EvenOdd/Even2.118.760
61OOOOO53.130
72OOOOE65.780
83OOOEO65.780
94OOOEE65.780
105OOEOO65.780
116OOEOE80.730
127OOEEO65.780
138OOEEE65.780
149OEOOO65.780
1510OEOOE80.730
1611OEOEO80.730
1712OEOEE80.730
1813OEEOO65.780
1914OEEOE80.730
2015OEEEO65.780
2116OEEEE65.780
2217EOOOO53.130
2318EOOOE65.780
2419EOOEO65.780
2520EOOEE65.780
2621EOEOO65.780
2722EOEOE80.730
2823EOEEO65.780
2924EOEEE65.780
3025EEOOO53.130
3126EEOOE65.780
3227EEOEO65.780
3328EEOEE65.780
3429EEEOO53.130
3530EEEOE65.780
3631EEEEO53.130
3732EEEEE53.130
38
39
Hoja2
Cell Formulas
RangeFormula
W5W5=SUM(W6:W37)

When I tested with version 2000 6 numbers-O/E permutations which have 53130 combination worked fine because it is less than the max row permitted 65536. Rest 6 O/E permutations which have 65780 and 20 others 80730 the highlight following line in yellow.
VBA Code:
CombinationArray(CombinationCounter, 1) = Ball_1 ' Save Ball_1 into the CombinationArray

Does it is possible to make it possible after 65000 rows the code could continue to next 5 Columns J:N?

Please make it possible if it can be done.

Thank you for your hard work I appreciate a lot.

Regards,
Kishan :)
 
Last edited:
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Replace the "Sub List5of50ByOddAndEvenDesignation(ByVal OddEvenString As String)" with the following code:

VBA Code:
Sub List5of50ByOddAndEvenDesignation(ByVal OddEvenString As String)
'
    Dim Ball_1                      As Long, Ball_2     As Long, Ball_3     As Long, Ball_4     As Long, Ball_5     As Long
    Dim CombinationCounter          As Long
    Dim OutputColumnNumber          As Long
    Dim Ball_1_Test                 As String, Ball_2_Test  As String, Ball_3_Test  As String, Ball_4_Test  As String, Ball_5_Test  As String
    Dim OddEvenCompareString        As String
    Dim CombinationArray()          As Variant
    Dim ws                          As Worksheet
'
    Const BallsToDraw As Long = 5                                                       ' <--- Set this to the # of balls to be drawn each iteration
    Const MaxWhiteBallValue As Long = 50                                                ' <--- Set this to the maximum # of balls being used
    Const MaxCombinationsPerColumn As Long = 65000                                      ' <--- Set this to the # of combinations to display before starting a new range
'
    Set ws = Sheets("Hoja2")                                                            ' <--- Set this to the name of the sheet that the data will go in
'
    OutputColumnNumber = 4                                                              ' <--- Set this the the # of the column to display results into
'
    ws.Range("D6:H" & Rows.Count).ClearContents                                         ' Clear the previous results
    ws.Range("J6:N" & Rows.Count).ClearContents                                         ' Clear the previous results
'
    ReDim CombinationArray(1 To MaxCombinationsPerColumn, 1 To 5)                       ' Establish the initial dimensions of the CombinationArray
'
    Application.ScreenUpdating = False                                                  ' Turn ScreenUpdating off
'
    For Ball_1 = 1 To MaxWhiteBallValue - 4                                             ' Loop for Ball_1 value
        For Ball_2 = Ball_1 + 1 To MaxWhiteBallValue - 3                                '   Loop for Ball_2 value
            For Ball_3 = Ball_2 + 1 To MaxWhiteBallValue - 2                            '       Loop for Ball_3 value
                For Ball_4 = Ball_3 + 1 To MaxWhiteBallValue - 1                        '           Loop for Ball_4 value
                    For Ball_5 = Ball_4 + 1 To MaxWhiteBallValue                        '               Loop for Ball_5 value
                        If Ball_1 Mod 2 = 0 Then                                        '                   If Ball_1 value is an even number then ...
                            Ball_1_Test = "E"                                           '                       Save 'E' to Ball_1_Test
                        Else                                                            '                   Else ...
                            Ball_1_Test = "O"                                           '                       Save 'O' to Ball_1_Test
                        End If
'
                        If Ball_2 Mod 2 = 0 Then                                        '                   If Ball_2 value is an even number then ...
                            Ball_2_Test = "E"                                           '                       Save 'E' to Ball_2_Test
                        Else                                                            '                   Else ...
                            Ball_2_Test = "O"                                           '                       Save 'O' to Ball_2_Test
                        End If
'
                        If Ball_3 Mod 2 = 0 Then                                        '                   If Ball_3 value is an even number then ...
                            Ball_3_Test = "E"                                           '                       Save 'E' to Ball_3_Test
                        Else                                                            '                   Else ...
                            Ball_3_Test = "O"                                           '                       Save 'O' to Ball_3_Test
                        End If
'
                        If Ball_4 Mod 2 = 0 Then                                        '                   If Ball_4 value is an even number then ...
                            Ball_4_Test = "E"                                           '                       Save 'E' to Ball_4_Test
                        Else                                                            '                   Else ...
                            Ball_4_Test = "O"                                           '                       Save 'O' to Ball_4_Test
                        End If
'
                        If Ball_5 Mod 2 = 0 Then                                        '                   If Ball_5 value is an even number then ...
                            Ball_5_Test = "E"                                           '                       Save 'E' to Ball_5_Test
                        Else                                                            '                   Else ...
                            Ball_5_Test = "O"                                           '                       Save 'O' to Ball_5_Test
                        End If
'
                        OddEvenCompareString = Ball_1_Test & Ball_2_Test & _
                                Ball_3_Test & Ball_4_Test & Ball_5_Test                 '                   Save all of the individual 'O's & 'E's into OddEvenCompareString
'
                        If OddEvenCompareString = OddEvenString Then                    '                   If OddEvenCompareString matches the string we are looking for then ...
                            CombinationCounter = CombinationCounter + 1                 '                       Increment CombinationCounter
'
                            CombinationArray(CombinationCounter, 1) = Ball_1            '                       Save Ball_1 into the CombinationArray
                            CombinationArray(CombinationCounter, 2) = Ball_2            '                       Save Ball_2 into the CombinationArray
                            CombinationArray(CombinationCounter, 3) = Ball_3            '                       Save Ball_3 into the CombinationArray
                            CombinationArray(CombinationCounter, 4) = Ball_4            '                       Save Ball_4 into the CombinationArray
                            CombinationArray(CombinationCounter, 5) = Ball_5            '                       Save Ball_5 into the CombinationArray
                        End If
'
                        If CombinationCounter = MaxCombinationsPerColumn Then           '                   If we have reached the Combination limit then
                            ws.Cells(6, OutputColumnNumber).Resize(UBound(CombinationArray, 1), _
                                    BallsToDraw).Value2 = CombinationArray              '                       Display the CombinationArray to the sheet
'
                            ReDim CombinationArray(1 To MaxCombinationsPerColumn, 1 To 5)   '                   Reset the CombinationArray
                            CombinationCounter = 0                                      '                       Reset the CombinationCounter
                            OutputColumnNumber = 10                                     '                       Update the OutputColumnNumber
                        End If
                    Next Ball_5                                                         '               Loop back
                Next Ball_4                                                             '           Loop back
            Next Ball_3                                                                 '       Loop back
        Next Ball_2                                                                     '   Loop back
    Next Ball_1                                                                         ' Loop back
'
    CombinationArray = ReDimPreserve(CombinationArray, CombinationCounter, BallsToDraw) ' Resize the CombinationArray to the actual # of rows used
'
' Output combinations
    ws.Cells(6, OutputColumnNumber).Resize(UBound(CombinationArray, 1), BallsToDraw).Value2 _
            = CombinationArray                                                          ' Display the CombinationArray to the sheet
'
    Application.ScreenUpdating = True                                                   ' Turn ScreenUpdating back on
End Sub


Make sure you keep the Function code that is below that code ... "Public Function ReDimPreserve(ArrayNameToResize, NewRowUbound, NewColumnUbound)"
 
Upvote 1
Solution
Replace the "Sub List5of50ByOddAndEvenDesignation(ByVal OddEvenString As String)" with the following code:

VBA Code:
Sub List5of50ByOddAndEvenDesignation(ByVal OddEvenString As String)
'
    Dim Ball_1                      As Long, Ball_2     As Long, Ball_3     As Long, Ball_4     As Long, Ball_5     As Long
    Dim CombinationCounter          As Long
    Dim OutputColumnNumber          As Long
    Dim Ball_1_Test                 As String, Ball_2_Test  As String, Ball_3_Test  As String, Ball_4_Test  As String, Ball_5_Test  As String
    Dim OddEvenCompareString        As String
    Dim CombinationArray()          As Variant
    Dim ws                          As Worksheet
'
    Const BallsToDraw As Long = 5                                                       ' <--- Set this to the # of balls to be drawn each iteration
    Const MaxWhiteBallValue As Long = 50                                                ' <--- Set this to the maximum # of balls being used
    Const MaxCombinationsPerColumn As Long = 65000                                      ' <--- Set this to the # of combinations to display before starting a new range
'
    Set ws = Sheets("Hoja2")                                                            ' <--- Set this to the name of the sheet that the data will go in
'
    OutputColumnNumber = 4                                                              ' <--- Set this the the # of the column to display results into
'
    ws.Range("D6:H" & Rows.Count).ClearContents                                         ' Clear the previous results
    ws.Range("J6:N" & Rows.Count).ClearContents                                         ' Clear the previous results
'
    ReDim CombinationArray(1 To MaxCombinationsPerColumn, 1 To 5)                       ' Establish the initial dimensions of the CombinationArray
'
    Application.ScreenUpdating = False                                                  ' Turn ScreenUpdating off
'
    For Ball_1 = 1 To MaxWhiteBallValue - 4                                             ' Loop for Ball_1 value
        For Ball_2 = Ball_1 + 1 To MaxWhiteBallValue - 3                                '   Loop for Ball_2 value
            For Ball_3 = Ball_2 + 1 To MaxWhiteBallValue - 2                            '       Loop for Ball_3 value
                For Ball_4 = Ball_3 + 1 To MaxWhiteBallValue - 1                        '           Loop for Ball_4 value
                    For Ball_5 = Ball_4 + 1 To MaxWhiteBallValue                        '               Loop for Ball_5 value
                        If Ball_1 Mod 2 = 0 Then                                        '                   If Ball_1 value is an even number then ...
                            Ball_1_Test = "E"                                           '                       Save 'E' to Ball_1_Test
                        Else                                                            '                   Else ...
                            Ball_1_Test = "O"                                           '                       Save 'O' to Ball_1_Test
                        End If
'
                        If Ball_2 Mod 2 = 0 Then                                        '                   If Ball_2 value is an even number then ...
                            Ball_2_Test = "E"                                           '                       Save 'E' to Ball_2_Test
                        Else                                                            '                   Else ...
                            Ball_2_Test = "O"                                           '                       Save 'O' to Ball_2_Test
                        End If
'
                        If Ball_3 Mod 2 = 0 Then                                        '                   If Ball_3 value is an even number then ...
                            Ball_3_Test = "E"                                           '                       Save 'E' to Ball_3_Test
                        Else                                                            '                   Else ...
                            Ball_3_Test = "O"                                           '                       Save 'O' to Ball_3_Test
                        End If
'
                        If Ball_4 Mod 2 = 0 Then                                        '                   If Ball_4 value is an even number then ...
                            Ball_4_Test = "E"                                           '                       Save 'E' to Ball_4_Test
                        Else                                                            '                   Else ...
                            Ball_4_Test = "O"                                           '                       Save 'O' to Ball_4_Test
                        End If
'
                        If Ball_5 Mod 2 = 0 Then                                        '                   If Ball_5 value is an even number then ...
                            Ball_5_Test = "E"                                           '                       Save 'E' to Ball_5_Test
                        Else                                                            '                   Else ...
                            Ball_5_Test = "O"                                           '                       Save 'O' to Ball_5_Test
                        End If
'
                        OddEvenCompareString = Ball_1_Test & Ball_2_Test & _
                                Ball_3_Test & Ball_4_Test & Ball_5_Test                 '                   Save all of the individual 'O's & 'E's into OddEvenCompareString
'
                        If OddEvenCompareString = OddEvenString Then                    '                   If OddEvenCompareString matches the string we are looking for then ...
                            CombinationCounter = CombinationCounter + 1                 '                       Increment CombinationCounter
'
                            CombinationArray(CombinationCounter, 1) = Ball_1            '                       Save Ball_1 into the CombinationArray
                            CombinationArray(CombinationCounter, 2) = Ball_2            '                       Save Ball_2 into the CombinationArray
                            CombinationArray(CombinationCounter, 3) = Ball_3            '                       Save Ball_3 into the CombinationArray
                            CombinationArray(CombinationCounter, 4) = Ball_4            '                       Save Ball_4 into the CombinationArray
                            CombinationArray(CombinationCounter, 5) = Ball_5            '                       Save Ball_5 into the CombinationArray
                        End If
'
                        If CombinationCounter = MaxCombinationsPerColumn Then           '                   If we have reached the Combination limit then
                            ws.Cells(6, OutputColumnNumber).Resize(UBound(CombinationArray, 1), _
                                    BallsToDraw).Value2 = CombinationArray              '                       Display the CombinationArray to the sheet
'
                            ReDim CombinationArray(1 To MaxCombinationsPerColumn, 1 To 5)   '                   Reset the CombinationArray
                            CombinationCounter = 0                                      '                       Reset the CombinationCounter
                            OutputColumnNumber = 10                                     '                       Update the OutputColumnNumber
                        End If
                    Next Ball_5                                                         '               Loop back
                Next Ball_4                                                             '           Loop back
            Next Ball_3                                                                 '       Loop back
        Next Ball_2                                                                     '   Loop back
    Next Ball_1                                                                         ' Loop back
'
    CombinationArray = ReDimPreserve(CombinationArray, CombinationCounter, BallsToDraw) ' Resize the CombinationArray to the actual # of rows used
'
' Output combinations
    ws.Cells(6, OutputColumnNumber).Resize(UBound(CombinationArray, 1), BallsToDraw).Value2 _
            = CombinationArray                                                          ' Display the CombinationArray to the sheet
'
    Application.ScreenUpdating = True                                                   ' Turn ScreenUpdating back on
End Sub


Make sure you keep the Function code that is below that code ... "Public Function ReDimPreserve(ArrayNameToResize, NewRowUbound, NewColumnUbound)"
Amazing johnnyL, you have ended up with a code very intelligently, 100% adapting as per my layout. Hats off to you for doing such a magical work that you have accomplished.👌

I am very touched by your work. You are so kind for giving best of the best solution. 🙌

I wish you good luck and cheers to your bright future. 🥂

Kind Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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