Cartesian Product 8 sets

ExcelAverage

New Member
Joined
Dec 10, 2012
Messages
32
Hi,

I know it's normally for two sets only, but how about as many as 8 sets. is that possible or any other way to perform that?

Thanks for all your help
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi Gsistek,
Thanks for your reply. I think it's quite big. One set contains 10 items and the least has two items. I have 8 sets of data. Please see this table.

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Single
[/TD]
[TD]Route
[/TD]
[TD]Bal
[/TD]
[TD]Trans
[/TD]
[TD]En
[/TD]
[TD]KL
[/TD]
[TD]W
[/TD]
[TD]E
[/TD]
[/TR]
[TR]
[TD]Dual
[/TD]
[TD]Analyze
[/TD]
[TD]Ali
[/TD]
[TD]Ods
[/TD]
[TD]Es
[/TD]
[TD]Na
[/TD]
[TD]9
[/TD]
[TD]H
[/TD]
[/TR]
[TR]
[TD]Four
[/TD]
[TD]Cascade
[/TD]
[TD]Both
[/TD]
[TD]TransOds
[/TD]
[TD]Fr
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]None
[/TD]
[TD]None
[/TD]
[TD]None
[/TD]
[TD]De
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Tr
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Si
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Fi
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Za
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Ru
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Pr
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
First combination would be - Single, Route, Bal, Trans, En, Kl, W, E
2. Single, Analyze, Bal, Trans, En, Kl, W, E and so on...

Combinations from left to right with column A as the base. So i want all the combinations for Single, Dual and Four.

Thanks again... Let me know if I missed something.

Thanks
 
Upvote 0
How do you want the output? Would a list of all combinations on a separate sheet work (with each element in a different column)? It would give you a table of 15,360 rows and 8 columns.
 
Upvote 0
Wow that was fast. Thanks Gsistek.
First, how did you come up with 15360? I knew I was wrong as i was able to arrive at only 5000+ records using concatenation. That is changing cell reference every time.

To make my spreadsheet flexible, as the data changes each time (columns and rows elements), I need the output be written at least on the 30th row of my excel. That's safe enough. Will you do this in excel? I found a macro program yesterday but that is good for 3 data with 3 elements each. I can't edit to make it suited for 8 sets with different element.

Thanks for your time...
 
Upvote 0
Wow that was fast. Thanks Gsistek.
First, how did you come up with 15360? I knew I was wrong as i was able to arrive at only 5000+ records using concatenation. That is changing cell reference every time.

To make my spreadsheet flexible, as the data changes each time (columns and rows elements), I need the output be written at least on the 30th row of my excel. That's safe enough. Will you do this in excel? I found a macro program yesterday but that is good for 3 data with 3 elements each. I can't edit to make it suited for 8 sets with different element.

Thanks for your time...

Try this out:
Code:
Sub CartProd8()
    Dim CartSize(1 To 8) As Long
    Dim I1 As Long, I2 As Long, I3 As Long, I4 As Long, _
        I5 As Long, I6 As Long, I7 As Long, I8 As Long
    Dim CurrentRow As Long, TotalRows As Long
'Disable Screen Updating (for speed)
    Application.ScreenUpdating = False
'Clear Current List
    Range(Cells(30, 1), Cells(Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row, 8)).Delete (xlShiftUp)
'Populate Size Array
    For I1 = 1 To 8
        CartSize(I1) = Cells(29, I1).End(xlUp).Row
    Next I1
'Determine Finished Size
    TotalRows = CartSize(1) * CartSize(2) * CartSize(3) * CartSize(4) * _
        CartSize(5) * CartSize(6) * CartSize(7) * CartSize(8)
'Start at Row 30
    CurrentRow = 30
'Copy Data - Loop Through Every Possibility
    For I1 = 1 To CartSize(1)
        For I2 = 1 To CartSize(2)
            For I3 = 1 To CartSize(3)
                For I4 = 1 To CartSize(4)
                    For I5 = 1 To CartSize(5)
                        For I6 = 1 To CartSize(6)
                            For I7 = 1 To CartSize(7)
                                For I8 = 1 To CartSize(8)
                                    Cells(CurrentRow, 1) = Cells(I1, 1)
                                    Cells(CurrentRow, 2) = Cells(I2, 2)
                                    Cells(CurrentRow, 3) = Cells(I3, 3)
                                    Cells(CurrentRow, 4) = Cells(I4, 4)
                                    Cells(CurrentRow, 5) = Cells(I5, 5)
                                    Cells(CurrentRow, 6) = Cells(I6, 6)
                                    Cells(CurrentRow, 7) = Cells(I7, 7)
                                    Cells(CurrentRow, 8) = Cells(I8, 8)
                                    CurrentRow = CurrentRow + 1
'Display Status
                                    Application.StatusBar = CurrentRow - 30 & "/" & TotalRows & _
                                        "  " & Format((CurrentRow - 30) / TotalRows, "0.0%")
                                Next I8
                            Next I7
                        Next I6
                    Next I5
                Next I4
            Next I3
        Next I2
    Next I1
'Clear StatusBar
    Application.StatusBar = ""
'Enable Screen Updating
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks very much for the time and effort. such a long code to build.
Surely, I will try this.

I so envy you for having this skills in VB codes.

Thanks again gsistek.
 
Upvote 0
I know this is an old thread - wondering if anyone has tried doing this for X number of sets? (possibly using recursion?) I'm looking to do this.
 
Upvote 0
I've toyed around with that. I just haven't had the time to sit down and write it out. I will definitely post it if I come up with something.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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