Make possible set of 5 numbers using from 3 or 4 groups

motilulla

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

Hello,

Here below VBA solution is given by Eric W which does create all possible combinations of 5 numbers from 5 groups picking 1 number from each group.
VBA Code:
Sub test1()
Dim UpperLeft As Range, Results As Range
Dim NumCols As Long, combos As Long, maxrow As Long, c As Long, r As Long
Dim cap() As Long, ix() As Long, output() As Variant, MyData As Variant

    Set UpperLeft = Range("B4")
    Set Results = Range("H4")
 
    NumCols = UpperLeft.End(xlToRight).Column - UpperLeft.Column + 1
    ReDim ix(1 To NumCols)
    ReDim cap(1 To NumCols)
    combos = 1
    maxrow = 0
 
    For c = 1 To NumCols
        cap(c) = UpperLeft.Offset(, c - 1).End(xlDown).Row - UpperLeft.Row
        combos = combos * cap(c)
        maxrow = IIf(maxrow > cap(c), maxrow, cap(c))
        ix(c) = 1
    Next c
    
    If combos > Rows.Count - Results.Row Then
        MsgBox "Insufficient rows to display all results"
        Exit Sub
    End If
 
    ReDim output(1 To combos, 1 To NumCols)
    MyData = UpperLeft.Offset(1).Resize(maxrow, NumCols).Value
 
    r = 1
 
NextItem:
    For c = 1 To NumCols
        output(r, c) = MyData(ix(c), c)
    Next c
    r = r + 1
 
    For c = NumCols To 1 Step -1
        ix(c) = ix(c) + 1
        If ix(c) <= cap(c) Then GoTo NextItem:
        ix(c) = 1
    Next c
 
    Results.Resize(, NumCols) = UpperLeft.Resize(, NumCols).Value
    Results.Offset(1).Resize(combos, NumCols) = output
 
End Sub

I need this VBA to finish my project analysis which can create all possible combination of 5 sets from 3 or 4 numbers groups (in case of 3 groups it has to pick from each group 3 numbers and in case of 4 groups it has to pick 2 numbers)

Here is an example with 4 group containing 3 numbers in each group to make it simpler Group columns B, C, D and E and result in the Columns H, I, J, K and L (it can be vary min 3 to max 7 numbers per group and the same min/max numbers would be in the 3 groups also)

MrExcel Question.xlsm
ABCDEFGHIJKLMN
1
2
3
4Group 1Group 2Group 3Group 4Group 5n1n2n3n4n5
514710124710
625811124711
736912124712
8
9134710
10134711
11134712
12
13234710
14234711
15234712
16
17245710
18245711
19245712
20
21345710
22345711
23345712
24
253691011
263691012
273691112
28
29
30
31
32
Sheet Modified


Please suggest VBA, I want which can work with Excel version 2000 also.

Thank you all.

Regards,
Moti
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hello, I did search in MrExcel with using lot of text in different way about lottery combinations by groups but no luck searched also in Google but did not find any solution.

Please need helping hand how can make a VBA which at least can make combinations of 5 set from “4 groups” picking 2 numbers from each group and 3 number from others 3 groups which will build a set of 5 numbers. Don’t know how much will be generated as per given example in the post#1

Thank you all in advance

Regards,
Moti
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Make possible set of 5 numbers using from 3 or 4 groups
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Hello Fluff, I am sorry.

Do you mean I post the whole question with details along with the cross post link?

Thank you

My Best Regards,
Moti
 
Upvote 0
No I mean that in future you need to let us know you have posted the same question elsewhere.
 
Upvote 0
No I mean that in future you need to let us know you have posted the same question elsewhere.
Ok Fluff, it is perfect i will follow-up Forum Rules.

This time i apologize and thank you for letting me know it is fair to inform here (y)

My Best Regards,
Moti :)
 
Last edited:
Upvote 0
Hello @Eric W,

Please can you help to create to get all possible combination of 5 numbers out of 4 groups picking minimum 2 numbers from the each or 4 groups.

I am sure after 150 views this question has no answer that means it is complicate task.

Please help. I have had intense search but did not find any answer yet.

Regards,
Moti
 
Upvote 0
Hello @Eric W,

Please can you help to create to get all possible combination of 5 numbers out of 4 groups picking minimum 2 numbers from the each or 4 groups.

I am sure after 150 views this question has no answer that means it is complicate task.

Please help. I have had intense search but did not find any answer yet.

Regards,
Moti
Hello,

I think perhaps my question is wrong please ignore I will re-think and summit again correcting it.

Sorry for the trouble thanks everyone for looking in to it.

Kind Regards,
Moti
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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