want to list/display all possible combinations

jcbasch

New Member
Joined
May 4, 2003
Messages
4
I'd like to display or list all possible combinations of data from 3 different columns. If for example there are 5 terms in column 1, 3 terms in column 2, and 2 terms in column 3, then I know the # of pos. combos , (5*3*2)=30. But how can I get excel to actually list and display all the combos?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi jcbasch:

Welcome to the Board!

How about posting some sample data to show waht you are working with. It will also help if you will post what formula (if any) that you have used, the results you got and what you think are the right results.
 
Upvote 0
The following is based on my solution here:

http://216.92.17.166/board2/viewtopic.php?t=44585

I'll put a new version of the code anyway....

Based on this worksheet:
Create unique strings with recursion.xls
ABCD
1ColumnAColumnBColumnC
2TermA1TermB1TermC1
3TermA2TermB2TermC2
4TermA3TermB3
5TermA4
6TermA5
Hoja1


I created the following scenario next to it:
Create unique strings with recursion.xls
EFGH
1Concat:TermA1+TermA2+TermA3+TermA4+TermA5,TermB1+TermB2+TermB3,TermC1+TermC2
2Separator:,
3Intermseparator:+
4
5Results:
Hoja1


where F1 is named 'Concat', F2 'Separator', F3 'InsSeparator' and F5 'Result'

The value of F1 was created using MCONCAT from MoreFunc.xll but that is not the important thing, the important is to create a string that has the same structure (Each member of the column separated by "InsSeparator" and those three strings separated by "Separator")

Next, I run the macro Test to get the results:<font face=Courier New><SPAN style="color:darkblue">Option</SPAN><SPAN style="color:darkblue">Explicit</SPAN><SPAN style="color:darkblue">Sub</SPAN> Test()
    <SPAN style="color:darkblue">Dim</SPAN> Ans<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Boolean</SPAN>, Col<SPAN style="color:darkblue">As</SPAN> Collection, i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>
    <SPAN style="color:darkblue">Set</SPAN> Col =<SPAN style="color:darkblue">New</SPAN> Collection
    Ans = BuildLoops(Range("Concat").Value, Range("Separator").Value, _
                     Range("InsSeparator").Value, Col)
    <SPAN style="color:darkblue">If</SPAN> Ans<SPAN style="color:darkblue">Then</SPAN>
        <SPAN style="color:darkblue">With</SPAN> Range("Result")
            <SPAN style="color:darkblue">For</SPAN> i = 1<SPAN style="color:darkblue">To</SPAN> Col.Count
                .Offset(i).Value = Col(i)
            <SPAN style="color:darkblue">Next</SPAN> i
        <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">With</SPAN>
    <SPAN style="color:darkblue">Else</SPAN>
        MsgBox "Error !", vbCritical
    <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Sub</SPAN><SPAN style="color:darkblue">Function</SPAN> BuildLoops(<SPAN style="color:darkblue">ByVal</SPAN> St<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
                    <SPAN style="color:darkblue">ByVal</SPAN> Sep<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
                    <SPAN style="color:darkblue">ByVal</SPAN> Sep2<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
                    <SPAN style="color:darkblue">ByRef</SPAN> Col<SPAN style="color:darkblue">As</SPAN> Collection)<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Boolean</SPAN>
    <SPAN style="color:darkblue">Dim</SPAN> Ar<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, Ar2<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, ArMain<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>
    <SPAN style="color:darkblue">Dim</SPAN> i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, j<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, Ctr<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, TempSt<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>
    St = Application.Substitute(St, " ", "")
    Ar = Split(St, Sep)
    <SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">Not</SPAN> IsArray(Ar)<SPAN style="color:darkblue">Then</SPAN><SPAN style="color:darkblue">Exit</SPAN><SPAN style="color:darkblue">Function</SPAN>
    <SPAN style="color:darkblue">ReDim</SPAN> ArMain(1<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(Ar) -<SPAN style="color:darkblue">LBound</SPAN>(Ar) + 1)
    <SPAN style="color:darkblue">For</SPAN> i =<SPAN style="color:darkblue">LBound</SPAN>(Ar)<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(Ar)
        Ar2 = Split(Ar(i), Sep2)
        Ctr = Ctr + 1
        ArMain(Ctr) = Ar2
    <SPAN style="color:darkblue">Next</SPAN> i
    <SPAN style="color:darkblue">For</SPAN> j =<SPAN style="color:darkblue">LBound</SPAN>(ArMain(1))<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(ArMain(1))
        TempSt = ArMain(1)(j)
        BuildString 1, ArMain, Col, TempSt, Sep
    <SPAN style="color:darkblue">Next</SPAN> j
    BuildLoops =<SPAN style="color:darkblue">True</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Function</SPAN><SPAN style="color:darkblue">Private</SPAN><SPAN style="color:darkblue">Function</SPAN> BuildString(<SPAN style="color:darkblue">ByRef</SPAN> i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, _
                            <SPAN style="color:darkblue">ByRef</SPAN> ArMain<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, _
                            <SPAN style="color:darkblue">ByRef</SPAN> Col<SPAN style="color:darkblue">As</SPAN> Collection, _
                            <SPAN style="color:darkblue">ByRef</SPAN> TempSt<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
                            <SPAN style="color:darkblue">ByRef</SPAN> Sep<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>)
    <SPAN style="color:darkblue">Dim</SPAN> j<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, St<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>
    St = TempSt
    <SPAN style="color:darkblue">If</SPAN> i<<SPAN style="color:darkblue">UBound</SPAN>(ArMain)<SPAN style="color:darkblue">Then</SPAN>
        <SPAN style="color:darkblue">For</SPAN> j =<SPAN style="color:darkblue">LBound</SPAN>(ArMain(i + 1))<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(ArMain(i + 1))
            TempSt = St & Sep & ArMain(i + 1)(j)
            BuildString i + 1, ArMain, Col, TempSt, Sep
        <SPAN style="color:darkblue">Next</SPAN> j
    <SPAN style="color:darkblue">Else</SPAN>
        Col.Add TempSt
    <SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Function</SPAN></FONT>
 
Upvote 0
The results I get (Not showing all 30...) are the following:
Create unique strings with recursion.xls
EFGH
1Concat:TermA1+TermA2+TermA3+TermA4+TermA5,TermB1+TermB2+TermB3,TermC1+TermC2
2Separator:,
3Intermseparator:+
4
5Results:
6TermA1,TermB1,TermC1
7TermA1,TermB1,TermC2
8TermA1,TermB2,TermC1
9TermA1,TermB2,TermC2
10TermA1,TermB3,TermC1
11TermA1,TermB3,TermC2
12TermA2,TermB1,TermC1
13TermA2,TermB1,TermC2
14TermA2,TermB2,TermC1
15TermA2,TermB2,TermC2
16TermA2,TermB3,TermC1
17TermA2,TermB3,TermC2
18TermA3,TermB1,TermC1
19TermA3,TermB1,TermC2
20TermA3,TermB2,TermC1
21TermA3,TermB2,TermC2
22TermA3,TermB3,TermC1
Hoja1


If you need to have each entry in a separate column, use Text To Columns after running the macro.
 
Upvote 0
Excel's COMBIN function indicates that there are 120 possible combinations.

COMBIN "Returns the number of combinations for a given number of items. Use COMBIN to determine the total possible number of groups for a given number of items" (from the Excel help file).

Syntax:
=COMBIN(Number of items, Size of the Group)

Number of items: 10
Size of the Group: 3

=COMBIN(10,3)
=120

Using Juan's data, you could have:

TermA1,TermB1,TermC1
TermA1,TermC1,TermB1
TermB1,TermA1,TermC1
TermC1,TermA1,TermB1

Etc.

I suppose it depends on how the OP defines "combination".

Regards,


Mike
 
Upvote 0
Mike, I got a 'thank you' PM, so I guess this approach worked.

And there's one flaw in your logic of the combinations, because you're assuming that all 10 items are part of the same group, where actually you have 3 different groups, each with a different number of items within.

The basic logic that the OP applied is the easiest (And fastest) to get the count of possible combinations:

ItemsInA * ItemsInB * ItemsInC

Or, if you want a more techincal and statistical approach:

Combinat(5, 1) * Combinat(3, 1) * Combinat(2, 1)

which just results in the same 30 items...
 
Upvote 0
Juan Pablo González said:
Mike, I got a 'thank you' PM, so I guess this approach worked.

And there's one flaw in your logic of the combinations, because you're assuming that all 10 items are part of the same group, where actually you have 3 different groups, each with a different number of items within.

The basic logic that the OP applied is the easiest (And fastest) to get the count of possible combinations:

ItemsInA * ItemsInB * ItemsInC

Or, if you want a more techincal and statistical approach:

Combinat(5, 1) * Combinat(3, 1) * Combinat(2, 1)

which just results in the same 30 items...


Hi Juan, actually I did receive an error when trying to run the macro yesterday, but wanted to try to work through it if I could.

I was able to assemble the concant:

TermA1+TermA2+TermA3+TermA4+TermA5,TermB1+TermB2+TermB3,TermC1+TermC2

I then copied your macro code into the vis basic module, ran the macro "test" and received a "run time error 1004 method 'range' of object of object' _global' failed"

when I clicked "debug" the vis basic highlighted this part of the code:

Ans = BuildLoops(Range("Concat").Value, Range("Separator").Value, _
Range("InsSeparator").Value, Col)

would you have any suggestions? I'm working on getting the download to display the actual sheets in html, but for now I hope this is a fairly clear explanation. using exel 2000
 
Upvote 0
Did you name the cell references ?

where F1 is named 'Concat', F2 'Separator', F3 'InsSeparator' and F5 'Result'

Select each cell, click on the name box (The one to the left of the formula bar), and put the name. Or change the code to use the "real" addresses, where it says Concat, put F1, where it says Separator put F2, etc.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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