All combinations of multiple columns of values?

rjvam

New Member
Joined
Nov 17, 2014
Messages
6
I have four columns of values. I am looking for a solution - be it formula, macro, or anything else! - to produce a list of all combinations of those four columns of values, separated by a character of my choosing.

For example:

ABCD
1HorseRed20"Web
2PigBlue30"Retail

<tbody>
</tbody>

I would want this solution to result in a list - on another sheet or document, the end destination doesn't matter to me - that would produce in this:

Horse-Red-20"-Web
Horse-Red-20"-Retail
Horse-Red-30"-Web
Horse-Red-30"-Retail
Horse-Blue-20"-Web
Horse-Blue-20"-Retail
Horse-Blue-30"-Web
Horse-Blue-30"-Retail
Pig-Red-20"-Web

And so on!

I tried to find an existing thread with this issue, but failed.

I am using Excel 2007. Thank you for your help and attention!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If you have more colum(s), it needs to modify.
Result will be in column F in same sheet.
Hope this helps.

Code:
Sub sample_rjvam()
Dim x, buf As String, C, keys
Dim i As Long, j As Long, k As Long, l As Long, LastR As Long
Set x = CreateObject("Scripting.Dictionary")
LastR = Cells(Rows.count, 1).End(xlUp).row
For Each C In Range(Range("A2"), Cells(LastR, 1))
    For i = 2 To LastR
        For j = 2 To LastR
            For k = 2 To LastR
                For l = 2 To LastR
                    buf = C & Cells(j, 2).Value & Cells(k, 3).Value & Cells(l, 4).Value
                        If Not x.Exists(buf) Then
                            x.add buf, buf
                        End If
                Next
            Next
        Next
    Next
Next
keys = x.keys
For i = 0 To x.count - 1
    Cells(i + 2, 6) = keys(i)
Next i
Set x = Nothing
End Sub
 
Last edited:
Upvote 0
A formula way, F1 copy down :

=IF(ROWS(A$1:A1)<17,IF(INT((ROWS($A$1:A1)-1)/8),A$2,A$1)&"-"&IF(MOD(INT((ROWS($A$1:A5)-1)/4)+1,2),B$2,B$1)&"-"&IF(MOD(INT((ROWS($A$1:A3)-1)/2)+1,2),C$2,C$1)&"-"&IF(MOD(ROWS($A$1:A2),2),D$2,D$1),"")

Regards
 
Upvote 0
Thank you all for your help! I appreciate it, and I appreciate multiple solutions as well.

Have a great day - you all have helped mine be better!
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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