Merge duplicate rows and sum column for each name

pdjm12

New Member
Joined
Jun 19, 2019
Messages
11
Hi

The data I'm trying to consolidate has this structure. I found pieces of code around the forum but i just can't make it work, is there a way using VBA or a formula I can do this?

Make this:

AB
John
1apple
2apple
1orange
Peter
1Lemon
1Orange
2Lemon
3Orange
Richard
1orange
1lemon
2apple
1apple
James
1lemon
1coconut
2apple
2coconut
1lemon

<tbody>
</tbody>
...

become this:

AB
John
3apple
1orange
Peter
3Lemon
4Orange
Richard
1orange
1lemon
3apple
James
2lemon
3coconut
2apple

<tbody>
</tbody>

Thank you
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this:-
NB:- This code will replace the data in columns "A & B".
Code:
[COLOR="Navy"]Sub[/COLOR] MG22Jun18
[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] R [COLOR="Navy"]As[/COLOR] Range, Ray(), c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] k [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]Set[/COLOR] Rng = Range("A:A").SpecialCells(xlCellTypeConstants)
ReDim Ray(1 To Lst, 1 To 2)

[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Areas
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Dn
        [COLOR="Navy"]If[/COLOR] Not .Exists(R.Offset(, 1).Value) [COLOR="Navy"]Then[/COLOR]
            .Add R.Offset(, 1).Value, R.Value
        [COLOR="Navy"]Else[/COLOR]
            .Item(R.Offset(, 1).Value) = .Item(R.Offset(, 1).Value) + R.Value
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] R
    
    c = c + 1
    Ray(c, 2) = Dn(1).Offset(-1, 1)
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] .keys
            c = c + 1
            Ray(c, 2) = k: Ray(c, 1) = .Item(k)
        [COLOR="Navy"]Next[/COLOR] k
   .RemoveAll
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
Range("A:B").ClearContents
Range("A1").Resize(c, 2).Value = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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