Convert a two column list (A,B) into unique rows based on value of A

cmaslowsky

New Member
Joined
May 10, 2019
Messages
3
I am looking for the most efficient way to take a two column table of data and convert it into a multi column table of only unique rows based on the value of the first column (see simple example below). Can be a VBA script or formulas. I am working with data that starts at around 10,000 rows of two columns, which will result in a list of about 1000 rows ranging from 1 associated result to 100 results, so I need to make sure it can process relatively efficiently.

Thanks!

START WITH:RESULT:
NUMBERCOLORNUMBERCOLORCOLORCOLORCOLOR...
1blue1bluered
1red2bluegreen
2blue3redgreenorange
2green4redyellow
3red5....
3green
3orange
4red
4yellow
5....

<tbody>
</tbody>
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:-
Data in columns "A & B" Results start "D1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG10May08
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Col [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Ray = Range(Range("A1"), Range("B" & Rows.Count).End(xlUp))
ReDim nray(1 To UBound(Ray, 1), 1 To UBound(Ray, 1))
nray(1, 1) = "Number"
nray(1, 2) = "Color"
c = 1
[COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
    [COLOR="Navy"]If[/COLOR] Not Ray(n, 1) = Temp [COLOR="Navy"]Then[/COLOR]
        Ac = 2: c = c + 1
        nray(c, 1) = Ray(n, 1)
        nray(c, 2) = Ray(n, 2)
    [COLOR="Navy"]Else[/COLOR]
        Ac = Ac + 1
         Col = IIf(Ac > Col, Ac, Col)
        nray(1, Ac) = "Color"
        nray(c, Ac) = Ray(n, 2)
    [COLOR="Navy"]End[/COLOR] If
Temp = Ray(n, 1)
[COLOR="Navy"]Next[/COLOR] n

Range("D1").Resize(c, Col).Value = nray
Regards Mick
 
Upvote 0
Perfect! Thanks Mick!

Try this:-
Data in columns "A & B" Results start "D1".
Code:
[COLOR=Navy]Sub[/COLOR] MG10May08
[COLOR=Navy]Dim[/COLOR] Ray [COLOR=Navy]As[/COLOR] Variant, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Ac [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Temp [COLOR=Navy]As[/COLOR] [COLOR=Navy]String,[/COLOR] Col [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
Ray = Range(Range("A1"), Range("B" & Rows.Count).End(xlUp))
ReDim nray(1 To UBound(Ray, 1), 1 To UBound(Ray, 1))
nray(1, 1) = "Number"
nray(1, 2) = "Color"
c = 1
[COLOR=Navy]For[/COLOR] n = 2 To UBound(Ray, 1)
    [COLOR=Navy]If[/COLOR] Not Ray(n, 1) = Temp [COLOR=Navy]Then[/COLOR]
        Ac = 2: c = c + 1
        nray(c, 1) = Ray(n, 1)
        nray(c, 2) = Ray(n, 2)
    [COLOR=Navy]Else[/COLOR]
        Ac = Ac + 1
         Col = IIf(Ac > Col, Ac, Col)
        nray(1, Ac) = "Color"
        nray(c, Ac) = Ray(n, 2)
    [COLOR=Navy]End[/COLOR] If
Temp = Ray(n, 1)
[COLOR=Navy]Next[/COLOR] n

Range("D1").Resize(c, Col).Value = nray
Regards Mick
 
Upvote 0
with PowerQuery

NUMBERCOLORNUMBERColor.1Color.2Color.3
1​
blue
1​
bluered
1​
red
2​
bluegreen
2​
blue
3​
redgreenorange
2​
green
4​
redyellow
3​
red
3​
green
3​
orange
4​
red
4​
yellow

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"NUMBER"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Color", each Table.Column([Count],"COLOR")),
    Extract = Table.TransformColumns(List, {"Color", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    SplitCount = Table.AddColumn(Extract, "Split Count", each List.Count(Text.Split([Color],","))),
    MaxCount = List.Max(SplitCount[Split Count]),
    Split = Table.SplitColumn(Extract, "Color", Splitter.SplitTextByDelimiter(","), MaxCount)
in
    Split[/SIZE]
 
Upvote 0
You could try explicitly naming the sheet as per code below:-
NB:Change "Sheet1" In second line to your data sheet name

If that does not work, you could post an example of data that failed.
Code:
[COLOR="Navy"]Sub[/COLOR] MG11May29
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Col [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
Ray = .Range("A1", .Range("B" & Rows.Count).End(xlUp))

ReDim nray(1 To UBound(Ray, 1), 1 To UBound(Ray, 1))
nray(1, 1) = "Number"
nray(1, 2) = "Color"
c = 1
[COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
    [COLOR="Navy"]If[/COLOR] Not Ray(n, 1) = Temp [COLOR="Navy"]Then[/COLOR]
        Ac = 2: c = c + 1
        nray(c, 1) = Ray(n, 1)
        nray(c, 2) = Ray(n, 2)
    [COLOR="Navy"]Else[/COLOR]
        Ac = Ac + 1
         Col = IIf(Ac > Col, Ac, Col)
        nray(1, Ac) = "Color"
        nray(c, Ac) = Ray(n, 2)
    [COLOR="Navy"]End[/COLOR] If
Temp = Ray(n, 1)
[COLOR="Navy"]Next[/COLOR] n

.Range("D1").Resize(c, Col).Value = nray
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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