Get Comma Delimed ID's if Column B is Duplicate

zmaniar

New Member
Joined
Sep 24, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. MacOS
Hello Guys. Need Help with a VBA

I have ID's in Column A and Name on Column B. I need to have Comma Seperated ID's if the name is same.
So in Column I I want it show "10, 120, 174, 410, 578, 600, 615, 625, 889"

10
APPLE​
120
APPLE​
174
APPLE​
410
APPLE​
578
APPLE​
600
APPLE​
615
APPLE​
625
APPLE​
889
APPLE​
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Are you sure you need to do it in VBA ? It is pretty straight forward to do that in Power Query.
In VBA unless you can rely on your data being sorted you need to be using the Dictionary object.

Here is an < 9 mins video on how to do it in Power Query from "The Power User".

20211207 PQ Concatenate Grouped Rows.xlsm
ABCDEFGHIJ
1IDNameNameIDs
210APPLEAPPLE10, 120, 174, 410, 578, 600, 615, 625, 889
3120APPLEORANGE20, 40, 60, 80, 100, 120
4174APPLE
5410APPLE
6578APPLE
7600APPLE
8615APPLE
9625APPLE
10889APPLE
1120ORANGE
1240ORANGE
1360ORANGE
1480ORANGE
15100ORANGE
16120ORANGE
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_ID"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Name", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Name", Order.Ascending}, {"ID", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Name"}, {{"ID Items", each _, type table [ID=nullable number, Name=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "IDs", each Table.Column([ID Items],"ID")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ID Items"}),
    #"Extracted Values" = Table.TransformColumns(#"Removed Columns", {"IDs", each Text.Combine(List.Transform(_, Text.From), ", "), type text})
in
    #"Extracted Values"
 
Upvote 0
Hi
Try this code
VBA Code:
Sub test()
    a = Cells(1).CurrentRegion
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If Not .exists(a(i, 2)) Then
                .Add a(i, 2), a(i, 1)
            Else
                .Item(a(i, 2)) = .Item(a(i, 2)) & "," & a(i, 1)
            End If
        Next
        Cells(1, 3).Resize(.Count, 2) = Application.Index(Application.Transpose(Array(.keys, .items)), 0, 0)
    End With
End Sub
 
Upvote 0
Hi
Try this code
VBA Code:
Sub test()
    a = Cells(1).CurrentRegion
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If Not .exists(a(i, 2)) Then
                .Add a(i, 2), a(i, 1)
            Else
                .Item(a(i, 2)) = .Item(a(i, 2)) & "," & a(i, 1)
            End If
        Next
        Cells(1, 3).Resize(.Count, 2) = Application.Index(Application.Transpose(Array(.keys, .items)), 0, 0)
    End With
End Sub
Hi,

Thank you for the code. I am getting run time error
 
Upvote 0
Attached Image
 

Attachments

  • Screenshot 2021-12-07 at 4.29.38 PM.png
    Screenshot 2021-12-07 at 4.29.38 PM.png
    215.1 KB · Views: 4
Upvote 0
The scripting dictionary won't work on Mac OS. You would need to instal an AddIn or Class Module to get the same functionality on Mac OS.
I'm afraid I do not know anymore about VBA on a Mac.
 
Upvote 0
You could also use a formula.
+Fluff 1.xlsm
ABCDE
1IDNameNameIDs
210APPLEAPPLE10, 120, 174, 410, 578, 600, 615, 625, 889
3120APPLEORANGE20, 40, 60, 80, 100, 120
4174APPLE
5410APPLE
6578APPLE
7600APPLE
8615APPLE
9625APPLE
10889APPLE
1120ORANGE
1240ORANGE
1360ORANGE
1480ORANGE
15100ORANGE
16120ORANGE
Master
Cell Formulas
RangeFormula
E2:E3E2=TEXTJOIN(", ",,IF($B$2:$B$16=D2,$A$2:$A$16,""))
 
Upvote 0
Solution
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,207,261
Messages
6,077,359
Members
446,279
Latest member
hoangquan2310

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