How to generate all possible combinations of two lists (without MACRO)?

erangai

New Member
Joined
Aug 22, 2012
Messages
6
Hi everyone,

I need to make a list of all possible combinations of two lists as shown in the following example.
i.e. Basically when I update Column A & B, the combinations should get appear in the “Output” column.
Also need to do this without running a MACRO.

Can someone kindly help me?

Example
Column A
Column B

Output (Combinations)
Mar
AA

MarAA
Apr
BB

MarBB
May
CC

MarCC

DD

MarDD



AprAA



AprBB



AprCC



AprDD



MayAA



MayBB



MayCC



MayDD
 
Last edited by a moderator:
Suppose the A and B data start with row 1.

Then, in some empty cell, say F1 enter the formula =IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))&INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))

Copy F1 down col. F until you get cells that look empty.
Thanks, could you give me the formua for 3-4-5 collumn? Thanks!!!
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the forum!

FYI, it's usually best to open a new thread with a question. If you add a message to an old thread, it's a big gamble as to whether someone will actually see it. tusharm hasn't posted anything for over 4 years, so it's unlikely he'll reply now.

Next, here's a formula that should work for multiple columns:

Cell Formulas
RangeFormula
H3:L38H3{=IF(ROW()>PRODUCT(SUBTOTAL(3,OFFSET($B$3:$B$20,0,COLUMN($B:$F)-COLUMN($B:$B))))+ROW($B$3)-1,"",INDEX(B:B,MOD(INT((ROW(H3)-ROW(H$3))/PRODUCT(SUBTOTAL(3,OFFSET($A$3:$A$20,0,COLUMN($H:H)-COLUMN($H:$H))))),COUNTA(B$3:B$20))+ROW(B$3)))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


You just need to make sure that there is one value in column A to make sure the formula works. Then copy the formula down and over as needed. To make it work with a different number of columns or rows, just adjust the ranges as needed. Or you can put a single element in the E and/or F columns and then ignore those columns in the output.

Hope this helps!
 
Upvote 0
Just use Power Query > Transform > Unpivot columns. Listing all possible combinations is extremely slow and error-prone. But if you use Power Query it is all done automatically.
 
Upvote 0
Suppose the A and B data start with row 1.

Then, in some empty cell, say F1 enter the formula =IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))&INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))

Copy F1 down col. F until you get cells that look empty.
@erangi
This is a fantastic formula. I cannot figure out how to add a space bewteen the resulting list so I want:
So to use the example
Column AColumn BOutput (Combinations)
MarAAMarAA

I want
Column AColumn BOutput (Combinations)
MarAAMar AA

I would also like to do a result in reverse order
Column AColumn BOutput (Combinations)
MarAAAA Mar

Any hlp modifying this formula for both scenarias would be apreciated.

ColinK2
 
Upvote 0
To add a space:

=IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))&" "&INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))

To reverse the order:

=IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))&" "&INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))
 
Upvote 0
To add a space:

=IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))&" "&INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))

To reverse the order:

=IF(ROW()-ROW($F$1)+1>COUNTA(A:A)*COUNTA(B:B),"",INDEX(B:B,MOD(ROW()-ROW($F$1),COUNTA(B:B))+1))&" "&INDEX(A:A,INT((ROW()-ROW($F$1))/COUNTA(B:B)+1))
Great Eric W - It works perfectly.

I wish I was smart enouigh to figure out how it works :-)

I read your comments on this page about this type of formula getting cumbersome and managed to get your Macro working.
Much more flexibility, can easily be edited for a Max number of roows and columns and order + it ignores cells with null or " " data

Cartesian using VBA Macro

This must be the best forum for Excel help compared to any IT forums.

Everytime I post a question I get a helpful reply.

I could nto consider asking this question on the unfreindly StackExchange were it seems users are supposed to know the answer before they ask the question and were there is no time for non experts.
 
Upvote 0
Glad I could help! :)

I don't spend much time on other forums, so I can't really compare. I do know that the moderators here spend a lot of effort promoting civility and collegiality. We get questions here from absolute beginners to people who have used Excel for decades and can't quite figure out something. Everyone's treated respectfully.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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