Concatenate Multiple Rows with same Unique Identifier

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
169
Hello,

Have a question for excel. I have a sheet with a large list of records. Each record has an ID, but is not unique due to multiple attributes which need to be accounted for, for each ID. Because these records are split, what I am trying to do is build another column at the end of my spreadsheet which will combine all the data from each unique ID into one cell. I will try to show below. Please notice, the combination of A2:B3 are duplicates, I wanted to include this in the example because I want to make sure if there are duplicates, I want to remove those from the results. I would put carriage returns in the results area.

A
B
C
1
ID
Type
Results
2
A-123
Cat-big
Cat-big
Cat-small
Dog-big
Dog-small
Dog-tiny
3
A-123
Cat-big
Cat-big
Cat-small
Dog-big
Dog-small
Dog-tiny

4
A-123
Cat-small
Cat-big
Cat-small
Dog-big
Dog-small
Dog-tiny

5
A-123
Dog-big
Cat-big
Cat-small
Dog-big
Dog-small
Dog-tiny

6
B-123
Dog-small
Dog-small
Dog-tiny

7
B-123
Dog-tiny
Dog-small
Dog-tiny

8
A-987
Cat-tiny
Cat-tiny
9
A-777
Cat-large
Cat-large
10
A-555
Bull-large
Bull-large
Bull-skinny
11
A-555
Bull-skinny
Bull-large
Bull-skinny


<tbody>
</tbody>
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
34,030
Office Version
365
Platform
Windows
Do you want to get rid of the duplicate IDs once you have concatenated the values in col B?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
34,030
Office Version
365
Platform
Windows
If you do want to delete the duplicates, how about
Code:
Sub kparadise()
   Dim Cl As Range, Rng As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Not Dic.Exists(Cl.Value) Then
         Dic.Add Cl.Value, Array(Cl.Offset(, 1), CreateObject("scripting.dictionary"))
         Dic(Cl.Value)(1)(Cl.Offset(, 1).Value) = Empty
      ElseIf Not Dic(Cl.Value)(1).Exists(Cl.Offset(, 1).Value) Then
         Dic(Cl.Value)(0).Value = Dic(Cl.Value)(0).Value & vbLf & Cl.Offset(, 1).Value
         Dic(Cl.Value)(1)(Cl.Offset(, 1).Value) = Empty
         If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
      Else
         If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
      End If
   Next Cl
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
End Sub
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
4,327
or with PowerQuery

IDType
A-123Cat-big
A-123Cat-big
A-123Cat-small
A-123Dog-big
B-123Dog-small
B-123Dog-tiny
A-987Cat-tiny
A-777Cat-large
A-555Bull-large
A-555Bull-skinny
IDResult
A-123Cat-big
Cat-small
Dog-big
B-123Dog-small
Dog-tiny
A-987Cat-tiny
A-777Cat-large
A-555Bull-large
Bull-skinny

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Grp = Table.Group(Source, {"ID"}, {{"Count", each _, type table}}),
    Lst = Table.AddColumn(Grp, "Result", each List.Distinct(Table.Column([Count],"Type"))),
    Ext = Table.TransformColumns(Lst, {"Result", each Text.Combine(List.Transform(_, Text.From), "#(lf)"), type text})
in
    Ext[/SIZE]
 

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
169
Do you want to get rid of the duplicate IDs once you have concatenated the values in col B?
No, I do not want to de-dup the entire sheet, I only want the results column to not include duplicates.
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
4,327
for Excel 2010/2013 this is free add-in
for Excel 2016 and above this is Get&Transform (PowerQuery) which is built-in
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,576
Office Version
2010
Platform
Windows
A
B
C
1
IDTypeResults
2
A-123Cat-bigCat-big
Cat-small
Dog-big
Dog-small
Dog-tiny
3
A-123Cat-bigCat-big
Cat-small
Dog-big
Dog-small
Dog-tiny
4
A-123Cat-smallCat-big
Cat-small
Dog-big
Dog-small
Dog-tiny
5
A-123Dog-bigCat-big
Cat-small
Dog-big
Dog-small
Dog-tiny
6
B-123Dog-smallDog-small
Dog-tiny
7
B-123Dog-tinyDog-small
Dog-tiny
8
A-987Cat-tinyCat-tiny
9
A-777Cat-largeCat-large
10A-555Bull-largeBull-large
Bull-skinny
11
A-555Bull-skinnyBull-large
Bull-skinny

<tbody>
</tbody>
Is your example result correct for the first four rows? I ask because you picked up the two Types for B-123 and concatenated them along with those from A-123. Originally I guessed that only the 123 mattered from the ID, but you did not do the same thing for the B-123 entries, so I am confused as to what IDs are used... unless, of course, you accidentally included the B-123 Types when you did not mean to.
 

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
169
Is your example result correct for the first four rows? I ask because you picked up the two Types for B-123 and concatenated them along with those from A-123. Originally I guessed that only the 123 mattered from the ID, but you did not do the same thing for the B-123 entries, so I am confused as to what IDs are used... unless, of course, you accidentally included the B-123 Types when you did not mean to.
You are correct, I apologize. I did not mean to include those. Dog-small and Dog-tiny are not supposed to be in there.

I DO NOT HAVE ACCESS TO POWERQUERY, FYI.
 
Last edited:

Forum statistics

Threads
1,086,220
Messages
5,388,550
Members
402,120
Latest member
dmitrevski

Some videos you may like

This Week's Hot Topics

Top