Using formula or macro to find non repeatable combinations

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am trying to get color combinations for various product types. I have the following two columns:

ProductColour
Product ABlue
Product AYellow
Product APink
Product AGreen
Product AOrange
Product BYellow
Product BGreen
Product BBlue
Product BPink
Product CBlue
Product CGreen
Product CPink
and so on.

I am looking to get a new table with the output being the following:

Product - Color 1 & Color 2

I would like for these combinations to be non repeatable. The output would look like the following:
ProductColour
Product ABlue & Yellow
Product ABlue & Pink
Product ABlue & Green
Product ABlue & Orange
Product AYellow & Pink
Product AYellow & Green
Product AYellow & Orange
......

Etc. Following it up with product B and C and so on.

How can I achieve this? I have tried some concatenating formulae that have 100 if statements, but I am not getting the desired results.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can use Power Query.

Book2
ABCDE
1ProductColourProductCustom
2Product ABlueProduct ABlue & Yellow
3Product AYellowProduct ABlue & Pink
4Product APinkProduct ABlue & Green
5Product AGreenProduct ABlue & Orange
6Product AOrangeProduct APink & Yellow
7Product BYellowProduct AGreen & Yellow
8Product BGreenProduct AOrange & Yellow
9Product BBlueProduct AGreen & Pink
10Product BPinkProduct AOrange & Pink
11Product CBlueProduct AGreen & Orange
12Product CGreenProduct BGreen & Yellow
13Product CPinkProduct BBlue & Yellow
14Product BPink & Yellow
15Product BBlue & Green
16Product BGreen & Pink
17Product BBlue & Pink
18Product CBlue & Green
19Product CBlue & Pink
20Product CGreen & Pink
X


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Link = Table.AddColumn(Source, "Link", each 1),
    Merge = Table.NestedJoin(Link, {"Link"}, Link, {"Link"}, "Added Custom", JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Merge, "Added Custom", {"Product", "Colour"}, {"Product.1", "Colour.1"}),
    SR = Table.SelectRows(Expand, each [Colour] <> [Colour.1] and [Product] = [Product.1]),
    Combo = Table.AddColumn(SR, "Custom", each if [Colour] < [Colour.1] then [Colour] & " & " & [Colour.1] else [Colour.1] & " & " & [Colour]),
    RC = Table.RemoveColumns(Combo,{"Colour", "Link", "Product.1", "Colour.1"}),
    RD = Table.Distinct(RC)
in
    RD
 
Upvote 0
Solution
And a VBA version if you prefer.

Book2
ABCDE
1ProductColourProductCOMBOS
2Product ABlueProduct ABlue & Yellow
3Product AYellowProduct ABlue & Pink
4Product APinkProduct ABlue & Green
5Product AGreenProduct ABlue & Orange
6Product AOrangeProduct AYellow & Pink
7Product BYellowProduct AYellow & Green
8Product BGreenProduct AYellow & Orange
9Product BBlueProduct APink & Green
10Product BPinkProduct APink & Orange
11Product CBlueProduct AGreen & Orange
12Product CGreenProduct BYellow & Green
13Product CPinkProduct BYellow & Blue
14Product BYellow & Pink
15Product BGreen & Blue
16Product BGreen & Pink
17Product BBlue & Pink
18Product CBlue & Green
19Product CBlue & Pink
20Product CGreen & Pink
Sheet4


VBA Code:
Sub Main()
Dim AR() As Variant:    AR = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")

COMBIX AR, 2, 1, 0, "", "", AL

With Range("D1:E1")
    .Value = Array("Product", "COMBOS")
    .Font.Bold = True
End With

With Range("D2").Resize(AL.Count)
    .Value = Application.Transpose(AL.toArray)
    .TextToColumns , DataType:=xlDelimited, SemiColon:=True
End With

End Sub

Sub COMBIX(AR() As Variant, Grp As Integer, IDX As Integer, Depth As Integer, Buffer As String, Prev As String, AL As Object)
Dim Prefix As String
Dim Product As String
Dim tmp As String

For i = IDX To UBound(AR)
    Product = AR(i, 1)
    If Buffer = vbNullString Then
        Prefix = AR(i, 2)
    Else
        If Prev = Product Then
            Prefix = Join(Array(Buffer, AR(i, 2)), " & ")
        End If
    End If
    If Depth + 1 = Grp Then
        If Prefix <> vbNullString Then
            tmp = Prev & ";" & Prefix
            If Not AL.contains(tmp) Then AL.Add tmp
        End If
    Else
        COMBIX AR, Grp, i + 1, Depth + 1, Prefix, Product, AL
    End If
Next i
End Sub
 
Upvote 0
Power Query is a very cool tool and well worth the time to get into.

Glad it worked for ya.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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