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:
Thank you. This works great. I tried applying it to another worksheet and I keep getting a blank versus an answer. The formula should work identical to the info I sent you. I guess I need to figure out why there is an error. I don't know if has anything to do with the dropdown lists for the dates. I will post once I figure this out.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi, I figured out what was causing my error. So now I have a new problem, if you look at the table on 2100400, as I mentioned this is a dropdown list of account numbers. Except, I forgot to mention that in that list, there will be one that says All. In order to display All accounts. So, basically your formula works great, it gives me the actual amount from the last column in the table below, for any given account from the dropdown list (see 2100400 below) in any given date range that I choose. However, when I select All from the dropdown of accounts, I get a blank. This is because what it does is, it looks at the cell below that says Transfer In210040042186 and it is now seeing Transfer InAll because when I change 2100400 on the dropdown to all it is concatenated, therefore, it'll change from Transfer In2100400 to Transfer InAll. Since Transfer In2100400 is technically reaching into column A below to find something that says Transfer InAll. This doesn't make sense because it's not going to find that. I want it to display all the Transferred In money from all the accounts. How do I incorporate All into your formula: =IFERROR(INDEX($H$2:$H$13,SMALL(IF($B$18=$A$2:$A$13,IF($D$2:$D$13>=$B$19,IF($D$2:$D$13<=$C$19,ROW($A$2:$A$13)-ROW($A$2)+1,""))),ROWS($B$18:B18))),"")
?
[TABLE="class: cms_table_cms_table_outer_border, width: 500, align: left"]
<tbody>[TR]
[TD]Concatenate[/TD]
[TD]Category[/TD]
[TD]Date[/TD]
[TD]Date[/TD]
[TD]Concatenate Date[/TD]
[TD]Acct. No.[/TD]
[TD]Actual Amt.[/TD]
[/TR]
[TR]
[TD]Operations100258042184[/TD]
[TD]Operations[/TD]
[TD]6/29/15[/TD]
[TD]6/29/15[/TD]
[TD]4218442184[/TD]
[TD]1002580[/TD]
[TD]0.18[/TD]
[/TR]
[TR]
[TD]Transfer In210040042186[/TD]
[TD]Transfer In[/TD]
[TD]7/1/15[/TD]
[TD]7/1/15[/TD]
[TD]4218642186[/TD]
[TD]2100400[/TD]
[TD]0.03[/TD]
[/TR]
[TR]
[TD]Capital310958642187[/TD]
[TD]Capital[/TD]
[TD]7/3/15[/TD]
[TD]7/3/15[/TD]
[TD]4218742187[/TD]
[TD]3109586[/TD]
[TD]0.24[/TD]
[/TR]
</tbody>[/TABLE]



[TABLE="class: cms_table_cms_table_outer_border, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Transfer In[/TD]
[TD]Transfer In210040042186[/TD]
[TD]0.03[/TD]
[/TR]
[TR]
[TD]2100400[/TD]
[TD]7/1/15 (42186 above)[/TD]
[TD]7/13/15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

 
Upvote 0
Try:

=IFERROR(INDEX($H$2:$H$13,SMALL(IF(($B$18=$A$2:$A$13)+($A$19="all"),IF($D$2:$D$13>=$B$19,IF($D$2:$D$13<=$C$19,ROW($A$2:$A$13)-ROW($A$2)+1,""))),ROWS($B$18:B18))),"")
 
Upvote 0
Come to think of it:

=IFERROR(INDEX($H$2:$H$13,SMALL(IF(($B$18=$A$2:$A$13)+(($A$19="all")*($C$2:$C$13=$A$18)),IF($D$2:$D$13>=$B$19,IF($D$2:$D$13<=$C$19,ROW($A$2:$A$13)-ROW($A$2)+1,""))),ROWS($B$18:B18))),"")

The last post will pick anything in the date range, this one will pick anything in the date range matching the category.
 
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.


Excellent sir. What will be the modification in the formula when we have values in same row in two different table? The number of entries are also not constant, for example Alphabets in table 1 and numbers in Table 2

Table 1 Table 2
ColA ColB ColC ColD ColE
AA BB 1 2 3
AC BD 5 6
AA 6

Please help me to sort this problem. I am trying to modify your formula, but not much success.
 
Upvote 0
Similar formula slightly different data

Hi, can i please get some help. I am trying to work out this formula, similar to the one i used earlier but now altered. In cell A2 I have concatenated data from cells A5 and B5. I did this because I have a mix of numbers and text. That formula lives in cell A2. In the category cell B4, that is a drop-down list with multiple categories, the same goes with cell A5, those will have multiple employees. Basically, what I would like to do is this, if I go to drop-down A5 and select a name, then go to category B5 and select anything in this case delivery on cell C5 give me the dates that are associated with that data. I know it needs to reference a second spreadsheet which will look like the chart next to this one.

Spreadsheet X
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[/TR]
[TR]
[TD]A2 Concatenated Data A5, B5[/TD]
[TD]B2 [/TD]
[TD]C2 [/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A3 [/TD]
[TD]B3[/TD]
[TD]C3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A4 EMPLOYEE[/TD]
[TD]B4 CATEGORY[/TD]
[TD]C4 DATES[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A5 204Smith[/TD]
[TD]B5 Delivery[/TD]
[TD]C5 [/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Spreadsheet X, tab XYZ
[TABLE="width: 500"]
<tbody>[TR]
[TD]Employee[/TD]
[TD]Delivery[/TD]
[TD]Pick-Up[/TD]
[TD]Purchase[/TD]
[/TR]
[TR]
[TD]204Smith[/TD]
[TD]1/12/18[/TD]
[TD][/TD]
[TD]2/10/18[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]







I would like something like this, I select 204Smith from the A5 drop-down menu and then Delivery from B5, therefore being referenced in cell A2 since it's concatenated and it should go into the same spreadsheet but tab xyz and go to cell A2 and then column and cell B2 and give me the date of 1/12/18 from that cell. I want it to do this everytime I changed the category. So, in other words, if I select purchase instead of delivery i want it to give the date 2/10/18 on cell C5.
 
Upvote 0
A fantastic solution, Eric! It works like a charm.

However, my dataset is massive and I was trying to find a way to edit the OutputCol variable so it continues to run the rest of the results after it reaches maxsize on this line: If mysize > 1000000 Then

I assume around 140 million results.

I have a solution programmed in JavaScript, but it crashes every browser (obviously), and I really don't want to try and provision a big data server XD. Do you think Excel can accomplish.
 
Upvote 0
I know this is an older thread, but how can I get the VBA editor to display the results in another column in the same worksheet when it runs out of rows. There are only 1,048,576 rows available. If I put my data in columns A1 through J90, there is definitely not enough rows to display the data. Even more so if I use A1 through F10, there are not enough rows.

Yes, it is possible to do. I even started to develop the formulas . . . then I came to my senses! :) Formulas like these are a good way to learn Excel, or as an intellectual exercise, but they are really not suited for genuine work. They are too big, too complicated, and not very flexible. Something like this can be done much better with a macro. I developed one that should do what you want.

1) Open Excel.
2) Press Alt-F11 to open the VBA editor.
3) Select Insert --> module from the menu.
4) Paste in the following code:
Code:
Sub Combos()
Dim Element(), Index()
Dim MyCols As Variant, MySheet As Worksheet, OneCol As Boolean
Dim r As Long, c As Long, ctr As Long, mysize As Long
Dim delim As String, OutputCol As String, str1 As String

' Set up conditions
    Set MySheet = Sheets("Sheet1")
    MyCols = Array("A", "B", "C")
    OutputCol = "F"
    OneCol = True
    delim = " - "
    
' resize the arrays
    ReDim Element(255, UBound(MyCols))
    ReDim Index(UBound(MyCols))
    
' Read the elements
    For c = 0 To UBound(MyCols)
        Element(0, c) = 0
        Index(c) = 1
        For r = 1 To 255
            If MySheet.Cells(r, MyCols(c)) <> "" Then
                Element(0, c) = Element(0, c) + 1
                Element(Element(0, c), c) = MySheet.Cells(r, MyCols(c))
            End If
        Next r
    Next c
    
' Clear the output columns(s), and check for the number of results
    ctr = MySheet.Cells(1, OutputCol).Column
    mysize = 1
    For c = 0 To UBound(MyCols)
        mysize = mysize * Element(0, c)
        MySheet.Columns(ctr).ClearContents
        ctr = ctr + 1
    Next c
    If mysize > 1000000 Then
        MsgBox "The number of results is too big to handle!"
        Exit Sub
    End If
    
    ctr = 0
    
' Start creating combinations
Loop1:
    ctr = ctr + 1
    str1 = ""
    Set resultcell = MySheet.Cells(ctr, OutputCol)
    For c = 0 To UBound(MyCols)
        If OneCol Then
            str1 = str1 & Element(Index(c), c) & delim
        Else
            resultcell.Value = Element(Index(c), c)
            Set resultcell = resultcell.Offset(0, 1)
        End If
    Next c
    If OneCol Then MySheet.Cells(ctr, OutputCol) = Left(str1, Len(str1) - Len(delim))
    
' Increment the indices
    For c = 0 To UBound(MyCols)
        Index(c) = Index(c) + 1
        If Index(c) <= Element(0, c) Then Exit For
        Index(c) = 1
    Next c
    If c <= UBound(MyCols) Then GoTo Loop1:
    
End Sub
5) Customize it to your needs. The first 5 executable lines (under 'Set up conditions") allow you to select which columns have data, where you want to put the results, and if you want the results to be in one column or multiple columns.
6) Return to Excel.
7) Set up your data. An empty cell, or one that has "" in it from a formula will be ignored.
8) Press Alt-F8, select Combos, the click Run.

That should do it. Good luck!
 
Upvote 0

Forum statistics

Threads
1,224,943
Messages
6,181,908
Members
453,071
Latest member
Gizmo2024

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