Sum duplicate row data and keep only 1 row

Excel_Blonde

New Member
Joined
Aug 8, 2018
Messages
44
Hi there,

I'm trying to manipulate some code (At bottom) I found within another thread on this site to meet my personal requirements.

Can someone explain what the various bits do please?

(Additional Info: I have a file with 28 columns (A:AF), I'm trying to look at 3 columns (A, F & H) and if the data in all 3 match any other rows I want to sum the value from multiple columns (E, R:U, W:X) and delete all rows except from one. The example below gives an indication of what I'm trying to achieve but doesn't reflect exactly how my data is presented.)

Ref1#to sumRef2#to sumRef3#to sum#to sumExpected Action
1A10
1A20
1A30
3B10All 3 columns match Row below so Merge/sum
3B10All 3 columns match row above so Merge/sum with row above and delete row
3B20
5A10
5A20
5A30

<tbody>
</tbody>



Sub CombineSumDuplicates_RegNbr()
' hiker95, 09/18/2016, ME965464
Dim lr As Long, r As Long, n As Long, t As Double
Application.ScreenUpdating = False
With Sheets("Sheet1")
lr = .Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
n = Application.CountIf(.Columns(2), .Cells(r, 2).Value)
If n > 1 Then
t = Application.Sum(.Cells(r, 4), .Cells(r + n - 1, 4))
.Cells(r, 4).Value = t
t = Application.Sum(.Cells(r, 5), .Cells(r + n - 1, 5))
.Cells(r, 5).Value = t
.Range(.Cells(r + 1, 1), .Cells(r + n - 1, 5)).ClearContents
End If
r = r + n - 1
Next r
On Error Resume Next
.Range("A3:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End With
Application.ScreenUpdating = True End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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