Reduce rows for same customer id

Kwanjau

New Member
Joined
Apr 16, 2019
Messages
3
I have two columns, on the first 1000 customer numbers and in the second an account number.
About 500 customers have multiple account numbers.
I want to reduce the lines.
I want only one line per customer number and in column two a string of the corresponding account numbers delimited by the & symbol.

Is there a multiple step way or do i require a macro?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
An example
Customer id - account number
Cust1 - acc01
Cust2 - acc02
Cust3 - acc03
Cust3 - acc04
Cust3 - acc05
.
.
Cust500 - acc1999
Cust500 - acc2000

Aim:
Cust1 - acc01
Cust2 - acc02
Cust3 - acc03&acc04&acc05
.
.
Cust500 - acc1999&acc2000
 
Upvote 0
Maybe this, assuming Columns "A" & "B"

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Cells(r, 1).Value = Cells(r - 1, 1).Value Then
        Cells(r - 1, 2).Value = Cells(r - 1, 2).Value & "&" & Cells(r, 2).Value
        Rows(r).Delete
    End If
Next r
End Sub
 
Upvote 0
Maybe this, assuming Columns "A" & "B"

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Cells(r, 1).Value = Cells(r - 1, 1).Value Then
        Cells(r - 1, 2).Value = Cells(r - 1, 2).Value & "&" & Cells(r, 2).Value
        Rows(r).Delete
    End If
Next r
End Sub

Thank you, I'll try it out.
 
Upvote 0
also you can try PowerQuery aka Get&Transform

Customer idaccount numberCustomer idaccount number
Cust1acc01Cust1acc01
Cust2acc02Cust2acc02
Cust3acc03Cust3acc03&acc04&acc05
Cust3acc04Cust500acc1999&acc2000
Cust3acc05
Cust500acc1999
Cust500acc2000

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    Group = Table.Group(Source, {"Customer id"}, {{"Count", each _, type table}}),
    TblList = Table.AddColumn(Group, "account number", each Table.Column([Count],"account number")),
    Extract = Table.TransformColumns(TblList, {"account number", each Text.Combine(List.Transform(_, Text.From), "&"), type text})
in
    Extract[/SIZE]
 
Upvote 0

Forum statistics

Threads
1,217,065
Messages
6,134,394
Members
449,870
Latest member
yuppies466

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