VBA to Concatenate one column data in excel and dedupe additional line items

Beth0331

New Member
Joined
Jun 13, 2018
Messages
7
Hello, Apologies if this is already on here - I did a search but was not successful in finding anything that is exactly what I'm looking for (and my VBA knowledge is limited...)

I am looking to take data I have in an excel spreadsheet and concatenate one column's data for a referenced "tag" (User ID below) in a another column. For example below, for all line items with the user ID jdoe3 I would like to combine the colors into one line and discard the remaining lines. Are you able to help me with the VBA code I would need to do this?

Currently it looks like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]User ID[/TD]
[TD]Colors[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]blue[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]yellow[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]orange[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]red[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]purple[/TD]
[/TR]
[TR]
[TD]Jane Smith[/TD]
[TD]jsmith5[/TD]
[TD]green[/TD]
[/TR]
[TR]
[TD]Jane Smith[/TD]
[TD]jsmith5[/TD]
[TD]orange[/TD]
[/TR]
[TR]
[TD]Jane Smith[/TD]
[TD]jsmith5[/TD]
[TD]red[/TD]
[/TR]
[TR]
[TD]George Phillips[/TD]
[TD]gphillips[/TD]
[TD]blue[/TD]
[/TR]
[TR]
[TD]George Phillips[/TD]
[TD]gphillips[/TD]
[TD]green[/TD]
[/TR]
</tbody>[/TABLE]


I would like it to look like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]User ID[/TD]
[TD]Colors[/TD]
[/TR]
[TR]
[TD]John Doe[/TD]
[TD]jdoe3[/TD]
[TD]blue, yellow, green, orange, red, purple[/TD]
[/TR]
[TR]
[TD]Jane Smith[/TD]
[TD]jsmith5[/TD]
[TD]green, orange, red[/TD]
[/TR]
[TR]
[TD]George Phillips[/TD]
[TD]gphillips[/TD]
[TD]blue, green[/TD]
[/TR]
</tbody>[/TABLE]

I'm using excel 2016

Thank You!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG13Jun34
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] nRng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
Txt = Dn.Value & Dn.Offset(, 1).Value
    [COLOR="Navy"]If[/COLOR] Not .Exists(Txt) [COLOR="Navy"]Then[/COLOR]
        .Add Txt, Dn
    [COLOR="Navy"]Else[/COLOR]
       .Item(Txt).Offset(, 2).Value = .Item(Txt).Offset(, 2).Value & ", " & Dn.Offset(, 3)
        [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] nRng = Dn Else [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you Mick! Quick question, why does the DN.offset at the end go to 3? What does this line tell the query to do?
.Item(Txt).Offset(, 2).Value = .Item(Txt).Offset(, 2).Value & ", " & Dn.Offset(, 3)
 
Upvote 0
Here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub DedupeAndConcatenate()
  Dim LR As Long, Ar As Range
  LR = Cells(Rows.Count, "A").End(xlUp).Row
  Range("A2:A" & LR) = Evaluate("IF(A2:A" & LR & "=A1:A" & LR - 1 & ","""",A2:A" & LR & ")")
  With Range("A1:A" & LR).SpecialCells(xlBlanks)
    For Each Ar In .Areas
      Ar(1).Offset(-1, 2) = Join(Application.Transpose(Ar.Offset(-1, 2).Resize(Ar.Count + 1)), ", ")
    Next
    .EntireRow.Delete
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Your quite right that should have been "Dn.offset(,2)"
It was just a typo Incurred when I was testing the code.
Well spotted
Regards Mick
 
Upvote 0
Hi Rick, thank you for this. It seems like both yours & Mick's macro's are based off of the Name column and not the user ID. When i tried to amend the code to look at column B, and cell B2 It works only if the user IDs are in order but not if they are out of order. Can you help me fix that?


Sub DedupeAndConcatenate()
Dim LR As Long, Ar As Range
LR = Cells(Rows.Count, "B").End(xlUp).Row
Range("B2:B" & LR) = Evaluate("IF(B2:B" & LR & "=B1:B" & LR - 1 & ","""",B2:B" & LR & ")")
With Range("B1:B" & LR).SpecialCells(xlBlanks)
For Each Ar In .Areas
Ar(1).Offset(-1, 1) = Join(Application.Transpose(Ar.Offset(-1, 1).Resize(Ar.Count + 1)), ", ")
Next
.EntireRow.Delete
End With
End Sub
 
Upvote 0
It works only if the user IDs are in order ..
That isn't too surprising since in post 1 you said "Currently it look like this:" and then gave sample data where the IDs were all grouped. ;)

Anyway, see if this does what you want. Test in a copy of your workbook.
Code:
Sub ConcatColors()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  With Range("A2", Range("C" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      d(a(i, 1) & "," & a(i, 2)) = d(a(i, 1) & "," & a(i, 2)) & ", " & a(i, 3)
    Next i
    .ClearContents
    With .Columns(1).Resize(d.Count)
      .Value = Application.Transpose(d.Keys)
      .TextToColumns DataType:=xlDelimited, Comma:=True, Space:=False, Other:=False
    End With
    With .Columns(3).Resize(d.Count)
      .Value = Application.Transpose(d.Items)
      .TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(2, 1))
      .Columns.AutoFit
    End With
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,168
Members
452,615
Latest member
bogeys2birdies

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