how to automatically sum and merge rows based on same value in column?

chezchez

New Member
Joined
Nov 30, 2012
Messages
2
hello, can anyone help me perform the following calculation in excel please?

I have a long list of items and their quantities, example:
[TABLE="width: 128"]
<tbody>[TR]
[TD="width: 64, align: right"]101001[/TD]
[TD="width: 64, align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101001[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101001[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]101001[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101002[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101002[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101003[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101004[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101005[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]1[/TD]
[/TR]
</tbody>[/TABLE]

now i would like to merge all the rows with the same value in column A and add up the corresponding values in column B to return the following:


[TABLE="width: 128"]
<tbody>[TR]
[TD="width: 64, align: right"]101001[/TD]
[TD="width: 64, align: right"]10[/TD]
[/TR]
[TR]
[TD="align: right"]101002[/TD]
[TD="align: right"]7[/TD]
[/TR]
[TR]
[TD="align: right"]101003[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101004[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101005[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]101050[/TD]
[TD="align: right"]13[/TD]
[/TR]
</tbody>[/TABLE]

I am guessing I have to run a macro, but I have no idea how to do that... any help would be much appreciated!
thanks, chez
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the board.

You can achieve this without a macro, you would use Data->Advanced to create a list of unique values and then a SUMIF function to total the values for each unique item
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG30Nov12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] nRng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), 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
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Offset(, 1)
    [COLOR="Navy"]Else[/COLOR]
        .Item(Dn.Value).Value = .Item(Dn.Value).Value + Dn.Offset(, 1)
            [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] nRng = Dn
            [COLOR="Navy"]Else[/COLOR]
                [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
            [COLOR="Navy"]End[/COLOR] If
    [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
wow, this is amazing!!!! thanks so much!!! just saved me hours of work!!
the first option with the IFSUM worked a gem! didn't make it to the other one, but thanks to both of you!! much appreciated!!!
 
Upvote 0
Is it possible to get this to not just do this for column B but loop until last column?

Try this:-
Code:
[COLOR=Navy]Sub[/COLOR] MG30Nov12
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] nRng [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A1"), 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
    [COLOR=Navy]If[/COLOR] Not .Exists(Dn.Value) [COLOR=Navy]Then[/COLOR]
        .Add Dn.Value, Dn.Offset(, 1)
    [COLOR=Navy]Else[/COLOR]
        .Item(Dn.Value).Value = .Item(Dn.Value).Value + Dn.Offset(, 1)
            [COLOR=Navy]If[/COLOR] nRng [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR]
                [COLOR=Navy]Set[/COLOR] nRng = Dn
            [COLOR=Navy]Else[/COLOR]
                [COLOR=Navy]Set[/COLOR] nRng = Union(nRng, Dn)
            [COLOR=Navy]End[/COLOR] If
    [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
Did you want the code to delete duplicates in individual columns , or duplicates within the whole range.???
 
Upvote 0
Thanks for the quick response. I would like it to delete duplicates within the whole range, after summing like values in column B. Column Header always starts at B4 typically to M as last column but could change.
 
Upvote 0
On reviewing my code I see it is intended to delete rows and amalgamate the duplicate offset data, because of the multiple columns now involved, I'm not totally sure what your Data and Results should look like.
Can you show a simple example of your data and expected results.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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