VBA generate 5_50 combinations from (5 groups)

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I want a VBA that can generate 5_50 lottery combinations out of 5 groups are in range A6:E15 (Generate set of 5 numbers but pick only “1 number” from each of 5 groups)…generate combinations in columns G:K.

For example 1, 2, 3, 4, 5 is not correct because this has 5 numbers from only group, … but can be good 1, 11, 21, 31, 41 one number from each group.

Here below is short example. Is it possible?

Excel Questions.xlsm
ABCDEFGHIJKL
1
2
3
4
5Gr1Gr2Gr3Gr4Gr5n1n2n3n4n5
6111213141111213141
7212223242111213142
8313233343111213143
9414243444111213144
10515253545111213145
11616263646111213146
12717273747111213147
13818283848111213148
14919293949111213149
151020304050111213150
16111213241
17111213242
18111213243
19111213244
20111213245
21111213246
22111213247
23111213248
24111213249
25111213250
26111213341
27111213342
28111213343
29111213344
30111213345
31111213346
32111213347
33111213348
34111213349
35111213350
36
37
38
39
40
1 Number By Group


Regards,
Moti
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hello, My friend made me the code below accordingly post#1 requirement which is working well this create 100000 combinations picking 1 number from each group. Please try.

VBA Code:
Sub Pick_1Num_FromEachGroup()

Dim a As Long, b As Long, c As Long, d As Long, Num As Long
Dim Gr1, Gr2, Gr3, Gr4, Gr5, Res

Gr1 = Range("A6:A15")
Gr2 = Range("B6:B15")
Gr3 = Range("C6:C15")
Gr4 = Range("D6:D15")
Gr5 = Range("E6:E15")


ReDim Res(1 To 200000, 1 To 10)

For z = 1 To UBound(Gr1)
If Gr1(z, 1) = "" Then Exit For
    For a = 1 To UBound(Gr2)
        For b = 1 To UBound(Gr3)
            For c = 1 To UBound(Gr4)
                For d = 1 To UBound(Gr5)
                    Num = Num + 1
                    Res(Num, 1) = Gr1(z, 1)
                    Res(Num, 2) = Gr2(a, 1)
                    Res(Num, 3) = Gr3(b, 1)
                    Res(Num, 4) = Gr4(c, 1)
                    Res(Num, 5) = Gr5(d, 1)
                Next
            Next
        Next
    Next
Next

Range("G6").Resize(Num, 5) = Res

End Sub

I want bit more twist but which is not possible for him, so please I need expert help I want to pick as follow…

Option1-pick from…Gr1-0num, Gr2-0num, Gr3-0num, Gr4-1num, Gr5-4num this will create 2100 combinations.

Option2-pick from…Gr1-0num, Gr2-0num, Gr3-0num, Gr4-2num, Gr5-3num this will create 5400 combinations.

Please help to make it possible.

Regards,
Moti
 
Last edited:
Upvote 0
Hello, sorry I changed a layout for easier picks from cell values.

Present macro post#2 picks 1 number from each 5 groups and makes 100000 combinations.

I need to pick instead 1 number out of 5 groups (do not pick any number from first three groups) just pick 2 numbers from Gr4, & 3 numbers from Gr 5

Pick from…Gr1-0num, Gr2-0num, Gr3-0num, Gr4-2num, Gr5-3num this will create 5400 combinations.

I want code also pick group number selection from the sheet rang C3:G3 as now 0 0 0 2 3 from each group

Please help to make it possible.

Here is also the cross post link…where is the example file attached too.

Modify VBA to pick numbers from group.

New layout…

Modifiy Macro Pick From Group.xlsm
ABCDEFGHIJKLM
1
2PickPickPickPickPick
3Elements From Group->00023
4
5Gr1Gr2Gr3Gr4Gr5n1n2n3n4n5
61112131413132414243
72122232423132414244
83132333433132414245
94142434443132414246
105152535453132414247
116162636463132414248
127172737473132414249
138182838483132414250
149192939493132414344
1510203040503132414345
163132414346
173132414347
183132414348
193132414349
203132414350
213132414445
223132414446
233132414447
243132414448
253132414449
263132414450
273132414546
283132414547
293132414548
303132414549
313132414550
323132414647
333132414648
343132414649
353132414650
363132414748
373132414749
383132414750
393132414849
403132414850
413132414950
423132424344
433132424345
443132424346
453132424347
463132424348
473132424349
483132424350
493132424445
503132424446
513132424447
523132424448
533132424449
543132424450
553132424546
563132424547
573132424548
583132424549
593132424550
603132424647
613132424648
623132424649
633132424650
643132424748
653132424749
663132424750
673132424849
683132424850
693132424950
703132434445
713132434446
723132434447
733132434448
743132434449
753132434450
763132434546
773132434547
783132434548
793132434549
803132434550
813132434647
823132434648
833132434649
843132434650
853132434748
863132434749
873132434750
883132434849
893132434850
903132434950
Euromillone


Regards,
Moti
 
Upvote 0
Hello, sorry I changed a layout for easier picks from cell values.

Present macro post#2 picks 1 number from each 5 groups and makes 100000 combinations.

I need to pick instead 1 number out of 5 groups (do not pick any number from first three groups) just pick 2 numbers from Gr4, & 3 numbers from Gr 5

Pick from…Gr1-0num, Gr2-0num, Gr3-0num, Gr4-2num, Gr5-3num this will create 5400 combinations.

I want code also pick group number selection from the sheet rang C3:G3 as now 0 0 0 2 3 from each group

Please help to make it possible.

Here is also the cross post link…where is the example file attached too.

Modify VBA to pick numbers from group.

New layout…

Modifiy Macro Pick From Group.xlsm
ABCDEFGHIJKLM
1
2PickPickPickPickPick
3Elements From Group->00023
4
5Gr1Gr2Gr3Gr4Gr5n1n2n3n4n5
61112131413132414243
72122232423132414244
83132333433132414245
94142434443132414246
105152535453132414247
116162636463132414248
127172737473132414249
138182838483132414250
149192939493132414344
1510203040503132414345
163132414346
173132414347
183132414348
193132414349
203132414350
213132414445
223132414446
233132414447
243132414448
253132414449
263132414450
273132414546
283132414547
293132414548
303132414549
313132414550
323132414647
333132414648
343132414649
353132414650
363132414748
373132414749
383132414750
393132414849
403132414850
413132414950
423132424344
433132424345
443132424346
453132424347
463132424348
473132424349
483132424350
493132424445
503132424446
513132424447
523132424448
533132424449
543132424450
553132424546
563132424547
573132424548
583132424549
593132424550
603132424647
613132424648
623132424649
633132424650
643132424748
653132424749
663132424750
673132424849
683132424850
693132424950
703132434445
713132434446
723132434447
733132434448
743132434449
753132434450
763132434546
773132434547
783132434548
793132434549
803132434550
813132434647
823132434648
833132434649
843132434650
853132434748
863132434749
873132434750
883132434849
893132434850
903132434950
Euromillone


Regards,
Moti
Hello, I want to share solution from cross post link below may it help someone here.

Modify VBA to pick numbers from group. [SOLVED]

Here is a macro which will work perfect with post#3 as it is presented and the combination values can be changed from the range C3:G3.

Good Luck to everyone. 🍻

Kind Regards,
Moti :)

VBA Code:
Option Explicit

Sub test() 'By jindon
    Dim i As Long, ii As Long, iii As Long, iv As Long, n As Long
    Dim s(3) As String, x(1 To 100), temp
    Dim cn As Object, rs As Object
    With [c5].CurrentRegion
        .Cells(1, .Columns.Count + 2).CurrentRegion.Offset(1).ClearContents
        For i = 1 To .Columns.Count
            If .Cells(-1, i) > 0 Then
                If .Cells(-1, i) > 1 Then ReDim temp(1 To .Cells(-1, i))
                For ii = 1 To .Cells(-1, i)
                    n = n + 1: x(n) = Join(Array("C", i, ii), "_")
                    [c5].CurrentRegion.Offset(1).Columns(i).Name = x(n)
                    If .Cells(-1, i) > 1 Then temp(ii) = "[" & x(n) & "].F1"
                Next
                If ii > 2 Then
                    For iii = 1 To ii - 2
                        For iv = iii + 1 To ii - 1
                            s(3) = s(3) & IIf(s(3) <> "", " And ", "") & _
                            "(" & temp(iii) & " < " & temp(iv) & ")"
                        Next
                    Next
                End If
            End If
        Next
        If n = 0 Then Exit Sub
        For i = 1 To n
            s(0) = s(0) & IIf(s(0) <> "", ", ", "") & "[" & x(i) & "].F1"
            s(1) = s(1) & IIf(s(1) <> "", ", ", "") & "[" & x(i) & "]"
            s(2) = s(2) & IIf(s(2) <> "", " And ", "") & "[" & x(i) & "].F1 Is Not Null"
        Next
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        cn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & _
            ThisWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=No';"
        rs.Open "Select " & s(0) & " From " & s(1) & " Where " & s(2) & _
            IIf(Len(s(3)), " And ", "") & s(3) & " Order By " & s(0) & ";", cn, 3, 3, 1
        .Cells(2, .Columns.Count + 2).CopyFromRecordset rs
    End With
    For i = 1 To n
        ThisWorkbook.Names(x(i)).Delete
    Next
    Set cn = Nothing: Set rs = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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