VBA to Make Combinations with Dynamic Data

thenapolitan

Board Regular
Joined
Sep 5, 2014
Messages
52
Hey Guys,

I've been racking my brain and can't seem to figure out the coding logic for what I'm doing. Basically, what I want to do is get all the combinations for a subset of parts to eventually come up with a gap/tolerance stack equation (not automated, yet).

I ask the user how many unique parts exist (in the example below, the user entered 3, but could be any number). I then ask how many duplicates of each (in this case 3 of part 1, 3 of part 2, and 2 of part 3, but again, these numbers could vary). It populates TABLE 1 below.

I populate a 2D array like this, but I'm not sure this is really how it should be done.
part# of partsdims
133
232
321

<tbody>
</tbody>

Now, I want to make a matrix to show all combinations of those parts. There should be 18 combinations (3*3*2). And I want the matrix to look like TABLE 2.

I just can't wrap my head around how to get this to work for any number of unique parts, with any number of duplicates and any number of dimensions.

Any ideas?

Thanks,

Chris



TABLE 1
ABCDEFGHI
Part:1Number of Parts:3Number of Dimensions:3
Dim 1Dim 2Dim 3
Part 1 of 1:111112113
Part 2 of 1:121122123
Part 3 of 1:131132133
Part:2Number of Parts:3Number of Dimensions:2
Dim 1Dim 2
Part 1 of 2:211212
Part 2 of 2:221222
Part 3 of 2:231232
Part:3Number of Parts:2Number of Dimensions:1
Dim 1
Part 1 of 3:311
Part 2 of 3:321

<tbody>
</tbody>



TABLE 2
Combo #Part 1Part 2Part 3Part 1 Dim 1Part 1 Dim 2Part 1 Dim 3Part 2 Dim 1Part 2 Dim 2Part 3 Dim 1Gap Formula
1111111112113211212311
2112111112113211212321
3121111112113221222311
4122111112113221222321
5131111112113231232311
6132111112113231232321
7211121122123211212311
8212121122123211212321
9221121122123221222311
10222121122123221222321
11231121122123231232311
12232121122123231232321
13311131132133211212311
14312131132133211212321
15321131132133221222311
16322131132133221222321
17331131132133231232311
18332131132133231232321

<tbody>
</tbody>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Did you see the second tab where it gets done programmatically instead of through functions?
 
Upvote 0
Did you see the second tab where it gets done programmatically instead of through functions?

Yes, but it does not give the option to programmatically use more or less than the specified columns.

I think I am going to programmatically populated the FORMULAS side and see if I can make it work that way using the combination. Will definitely post results.

Ideally, I'd like to come up with a recursive function that will do all of this for an infinite number of cases. Here is my case statement right now (only works up to case 10). For each case, all it is is the addition of another For-to loop and an extra array. Kind of hefty without recursion. Just can't wrap my head around recursion though....

Code:
Sub CartesianProduct(MatrixSheet As Worksheet)
    Dim startrange As range


    Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, g As Integer, h As Integer, i As Integer, j As Integer
    Dim x As Integer, z As Integer


    x = Worksheets("Sheet1").range("D2")
    z = 0


    Select Case x
        Case 1
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            array1 = [range1]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                z = z + 1
                startrange.range("C2").Offset(z, 0).Value = array1(a, 1)
            Next a
        Case 2
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            
            array1 = [range1]
            array2 = [range2]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    z = z + 1
                    startrange.Offset(z, 0).Value = array1(a, 1)
                    startrange.Offset(z, 1).Value = array1(b, 1)
                Next b
            Next a
        Case 3
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            
            array1 = [range1]
            array2 = [range2]
            array3 = [range3]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        z = z + 1
                        startrange.Offset(z, 0).Value = array1(a, 1)
                        startrange.Offset(z, 1).Value = array1(b, 1)
                        startrange.Offset(z, 2).Value = array1(c, 1)
                    Next c
                Next b
            Next a
        Case 4
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            
            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            z = z + 1
                            startrange.Offset(z, 0).Value = array1(a, 1)
                            startrange.Offset(z, 1).Value = array1(b, 1)
                            startrange.Offset(z, 2).Value = array1(c, 1)
                            startrange.Offset(z, 3).Value = array1(d, 1)
                        Next d
                    Next c
                Next b
            Next a
        Case 5
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                z = z + 1
                                startrange.Offset(z, 0).Value = array1(a, 1)
                                startrange.Offset(z, 1).Value = array1(b, 1)
                                startrange.Offset(z, 2).Value = array1(c, 1)
                                startrange.Offset(z, 3).Value = array1(d, 1)
                                startrange.Offset(z, 3).Value = array1(e, 1)
                            Next e
                        Next d
                    Next c
                Next b
            Next a
        Case 6
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)
            range6 = Application.InputBox(Prompt:="Please Select Range 6", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]
            array6 = [range6]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                For f = 1 To UBound(array4)
                                    z = z + 1
                                    startrange.Offset(z, 0).Value = array1(a, 1)
                                    startrange.Offset(z, 1).Value = array1(b, 1)
                                    startrange.Offset(z, 2).Value = array1(c, 1)
                                    startrange.Offset(z, 3).Value = array1(d, 1)
                                    startrange.Offset(z, 3).Value = array1(e, 1)
                                    startrange.Offset(z, 3).Value = array1(f, 1)
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
        Case 7
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)
            range6 = Application.InputBox(Prompt:="Please Select Range 6", Type:=8)
            range7 = Application.InputBox(Prompt:="Please Select Range 7", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]
            array6 = [range6]
            array7 = [range7]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                For f = 1 To UBound(array4)
                                    For g = 1 To UBound(array4)
                                        z = z + 1
                                        startrange.Offset(z, 0).Value = array1(a, 1)
                                        startrange.Offset(z, 1).Value = array1(b, 1)
                                        startrange.Offset(z, 2).Value = array1(c, 1)
                                        startrange.Offset(z, 3).Value = array1(d, 1)
                                        startrange.Offset(z, 3).Value = array1(e, 1)
                                        startrange.Offset(z, 3).Value = array1(f, 1)
                                        startrange.Offset(z, 3).Value = array1(g, 1)
                                    Next g
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
        Case 8
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)
            range6 = Application.InputBox(Prompt:="Please Select Range 6", Type:=8)
            range7 = Application.InputBox(Prompt:="Please Select Range 7", Type:=8)
            range8 = Application.InputBox(Prompt:="Please Select Range 8", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]
            array6 = [range6]
            array7 = [range7]
            array8 = [range8]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                For f = 1 To UBound(array4)
                                    For g = 1 To UBound(array4)
                                        For h = 1 To UBound(array4)
                                            z = z + 1
                                            startrange.Offset(z, 0).Value = array1(a, 1)
                                            startrange.Offset(z, 1).Value = array1(b, 1)
                                            startrange.Offset(z, 2).Value = array1(c, 1)
                                            startrange.Offset(z, 3).Value = array1(d, 1)
                                            startrange.Offset(z, 3).Value = array1(e, 1)
                                            startrange.Offset(z, 3).Value = array1(f, 1)
                                            startrange.Offset(z, 3).Value = array1(g, 1)
                                            startrange.Offset(z, 3).Value = array1(h, 1)
                                        Next h
                                    Next g
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
        Case 9
                        range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)
            range6 = Application.InputBox(Prompt:="Please Select Range 6", Type:=8)
            range7 = Application.InputBox(Prompt:="Please Select Range 7", Type:=8)
            range8 = Application.InputBox(Prompt:="Please Select Range 8", Type:=8)
            range9 = Application.InputBox(Prompt:="Please Select Range 9", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]
            array6 = [range6]
            array7 = [range7]
            array8 = [range8]
            array9 = [range9]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                For f = 1 To UBound(array4)
                                    For g = 1 To UBound(array4)
                                        For h = 1 To UBound(array4)
                                            For i = 1 To UBound(array4)
                                                z = z + 1
                                                startrange.Offset(z, 0).Value = array1(a, 1)
                                                startrange.Offset(z, 1).Value = array1(b, 1)
                                                startrange.Offset(z, 2).Value = array1(c, 1)
                                                startrange.Offset(z, 3).Value = array1(d, 1)
                                                startrange.Offset(z, 3).Value = array1(e, 1)
                                                startrange.Offset(z, 3).Value = array1(f, 1)
                                                startrange.Offset(z, 3).Value = array1(g, 1)
                                                startrange.Offset(z, 3).Value = array1(h, 1)
                                                startrange.Offset(z, 3).Value = array1(i, 1)
                                            Next i
                                        Next h
                                    Next g
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a


        Case 10
            range1 = Application.InputBox(Prompt:="Please Select Range 1", Type:=8)
            range2 = Application.InputBox(Prompt:="Please Select Range 2", Type:=8)
            range3 = Application.InputBox(Prompt:="Please Select Range 3", Type:=8)
            range4 = Application.InputBox(Prompt:="Please Select Range 4", Type:=8)
            range5 = Application.InputBox(Prompt:="Please Select Range 5", Type:=8)
            range6 = Application.InputBox(Prompt:="Please Select Range 6", Type:=8)
            range7 = Application.InputBox(Prompt:="Please Select Range 7", Type:=8)
            range8 = Application.InputBox(Prompt:="Please Select Range 8", Type:=8)
            range9 = Application.InputBox(Prompt:="Please Select Range 9", Type:=8)
            range10 = Application.InputBox(Prompt:="Please Select Range 10", Type:=8)


            array1 = [range1]
            array2 = [range2]
            array3 = [range3]
            array4 = [range4]
            array5 = [range5]
            array6 = [range6]
            array7 = [range7]
            array8 = [range8]
            array9 = [range9]
            array10 = [range10]


            Set startrange = MatrixSheet.range("C1")
            For a = 1 To UBound(array1)
                For b = 1 To UBound(array2)
                    For c = 1 To UBound(array3)
                        For d = 1 To UBound(array4)
                            For e = 1 To UBound(array4)
                                For f = 1 To UBound(array4)
                                    For g = 1 To UBound(array4)
                                        For h = 1 To UBound(array4)
                                            For i = 1 To UBound(array4)
                                                For j = 1 To UBound(array4)
                                                    z = z + 1
                                                    startrange.Offset(z, 0).Value = array1(a, 1)
                                                    startrange.Offset(z, 1).Value = array1(b, 1)
                                                    startrange.Offset(z, 2).Value = array1(c, 1)
                                                    startrange.Offset(z, 3).Value = array1(d, 1)
                                                    startrange.Offset(z, 3).Value = array1(e, 1)
                                                    startrange.Offset(z, 3).Value = array1(f, 1)
                                                    startrange.Offset(z, 3).Value = array1(g, 1)
                                                    startrange.Offset(z, 3).Value = array1(h, 1)
                                                    startrange.Offset(z, 3).Value = array1(i, 1)
                                                    startrange.Offset(z, 3).Value = array1(j, 1)
                                                Next j
                                            Next i
                                        Next h
                                    Next g
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
    End Select
End Sub
 
Last edited:
Upvote 0
Really?

Did you try just adding additional columns of input to the right and pushing the button?

Row\Col
B​
C​
D​
E​
F​
4​
DumplingsKung Pao ShrimpFried riceFortune CookieAlan
5​
Egg rollOrange BeefSteamed riceIce CreamBarb
6​
Wonton soupPork Lo MeinRice noodlesCain
7​
Roast Duck
8​
Szechuan Chicken
9​
10​
DumplingsKung Pao ShrimpFried riceFortune CookieAlan
11​
DumplingsKung Pao ShrimpFried riceFortune CookieBarb
12​
DumplingsKung Pao ShrimpFried riceFortune CookieCain
13​
DumplingsKung Pao ShrimpFried riceIce CreamAlan
14​
DumplingsKung Pao ShrimpFried riceIce CreamBarb
15​
DumplingsKung Pao ShrimpFried riceIce CreamCain
16​
DumplingsKung Pao ShrimpSteamed riceFortune CookieAlan
17​
DumplingsKung Pao ShrimpSteamed riceFortune CookieBarb
18​
DumplingsKung Pao ShrimpSteamed riceFortune CookieCain
 
Upvote 0

Forum statistics

Threads
1,217,415
Messages
6,136,506
Members
450,016
Latest member
murarj

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