Merge Excel rows based on common column values

OC2PS

New Member
Joined
Jan 18, 2018
Messages
11
I have a table similar to the image below. Column A has repeated values, while column B has values corresponding to column A. e.g. a single customer can have multiple orders.
combine-duplicate-rows.png


What I would like to do is combine the rows, so that there is only one row per unique column A value (customer) and in the column B for that row, all column B values (orders) are gathered together in a comma separated list.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Sub OC2PS()
   Dim Ary As Variant
   Dim r As Long
   
   Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
   
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Ary(r, 2)
         Else
            .Item(Ary(r, 1)) = .Item(Ary(r, 1)) & ", " & Ary(r, 2)
         End If
      Next r
      Sheets("Sheet2").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
Change sheet names to suit.
 
Upvote 0
Solution
Thank you! Quick question: In the code, you've noted `Range("A1")` - should I provide the full data range in this? e.g. A2:B8
 
Upvote 0
No need, as long as there aren't any completely blank rows or columns in the data.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,755
Messages
6,180,776
Members
452,996
Latest member
nelsonsix66

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