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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Mostly, I don't understand what you are trying to do. My fault, not yours, I'm not a numbers guy.

I am wondering if a Cartesian Permutation worksheet would be of help?
One is fairly available on a search, I believe, or I can post a link to my example.

I enter you little grid...

1 3 3
2 3 2
3 2 1


And it produced this result in a split second. Where this is a concatenated end result with duplicates remove, if any.

1 3 3
1 3 2
1 3 1
1 2 3
1 2 2
1 2 1
2 3 3
2 3 2
2 3 1
2 2 3
2 2 2
2 2 1
3 3 3
3 3 2
3 3 1
3 2 3
3 2 2
3 2 1

The Table 1 & 2 are beyond my thought process.

Howard
 
Upvote 0
Hey L. Howard, thanks for the reply. I'll take a look at the Cartesian Permutation, but let me try to simplify a little. I think it's just too complicated with those "dimensions" in there.

Basically, for that example figure I have 3 unique mechanical parts that fit together in some way (but could be any number of parts that the user enters):
  • part 1 has 3 duplicates (don't worry about dimensions)
    • aka, we got 3 of these parts made at a manufacturer, but the user should be able to enter any number of duplicates, say, if we had 15 made or something
  • part 2 has 3 duplicates
  • part 3 has 2 duplicates

So - the total number of combinations is 18 (3*3*2). Below shows the combo number and the actual combination of parts that would fit together. It actually reminds me a little of binary counting, but varies depending on the number of duplicates the user enters.

Combo #1 would look at Part 1's first duplicate, Part 2's first duplicate, Part 3's first duplicate.
Combo #2 would look at Part 1's first duplicate, Part 2's first duplicate, Part 3's second duplicate.
Combo #3 would look at Part 1's first duplicate, Part 2's second duplicate, Part 3's first duplicate.

...and so on. The biggest problem I am facing is that I want to make this work for any number of parts with any number of duplicates.


Combo #Part 1Part 2Part 3
1111
2112
3121
4122
5131
6132
7211
8212
9221
10222
11231
12232
13311
14312
15321
16322
17331
18332

<tbody>
</tbody>
 
Upvote 0
Ok L. Howard! I just found a Cartesian Product thing! Made it work with the set of 3 parts. WORKS GREAT! This is very close to exactly what I want. The only problem is I want to make it work for a dynamic number of parts with a dynamic number of duplicates (I'm thinking to use recursion, but I have no idea how to made a function that will do that yet). Will post updates as I come up with them. See the code below that works for 3 parts (modified slightly from Creating a cartesian product in Excel with a Visual Basic macro - blog @ tootricky.co.uk):

Code:
Sub cartesianproduct()
    Dim startrange As Range
    
    range1 = Application.InputBox(Prompt:="Please Select First Range", Type:=8)
    range2 = Application.InputBox(Prompt:="Please Select Second Range", Type:=8)
    range3 = Application.InputBox(Prompt:="Please Select Third Range", Type:=8)
    Set startrange = Application.InputBox(Prompt:="Please select where you want to put it", Type:=8)
    
    array1 = [range1]
    array2 = [range2]
    array3 = [range3]
    
    startrange.Select
    For i = 1 To UBound(array1)
        For x = 1 To UBound(array2)
            For y = 1 To UBound(array3)
                Z = Z + 1
                ActiveCell.Offset(Z, 0).Value = array1(i, 1)
                ActiveCell.Offset(Z, 1).Value = array2(x, 1)
                ActiveCell.Offset(Z, 2).Value = array2(y, 1)
            Next y
        Next
    Next
End Sub
 
Last edited:
Upvote 0
It will works with as many as you enter, with up to 1 million results.
 
Upvote 0
It will works with as many as you enter, with up to 1 million results.

It looks like it can only have 4 categories (in this example "appetizer", "main course", "rice", "dessert"). Each category is a "set" for me. I want to be able allow the user to choose how many "sets" they want. Will the spreadsheet do this?
 
Upvote 0
Did you read this part of the instructions?

To increase the number of columns, insert columns in the middle of the table and copy the columns from the left or right to cover.
 
Upvote 0

Forum statistics

Threads
1,216,507
Messages
6,131,059
Members
449,616
Latest member
PsychoCube

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