Sum and Delete Duplicate Rows

RossH94

New Member
Joined
Jan 22, 2018
Messages
15
Hi All,

I need a table that shows the DriverName and Duration columns, every other column can be deleted. All duplicates must be delete and the duration for each driver should be summed.

I've been struggling with this for a while, the code i've been trying to use is below. It needs to be done in Vba.

If anyone can help I would greatly appreciate it.

Sub Test
Dim Rng As Range, Dn As Range, n As Long, nRng As Range
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Dn
Else
If nRng Is Nothing Then Set nRng = _
Dn Else Set nRng = Union(nRng, Dn)
.Item(Dn.Value).Offset(, 3) = .Item(Dn.Value).Offset(, 3) + Dn.Offset(, 3)
End If
Next
If Not nRng Is Nothing Then nRng.EntireRow.Delete
End With
End Sub


vba.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this :
Code:
Sub Test()
Dim rng As Range
[D1:D2] = [A1:A2].Value
[A:C,E:L].Delete
[C4] = "Duration"
Set rng = Range([C5], Cells(Rows.Count, "A").End(xlUp)(1, 3))
With rng
    .NumberFormat = "hh:mm:ss"
    .Formula = "=SUMIF(" & .Offset(0, -2).Address & ",A5," & .Offset(0, -1).Address & ")"
    .Value = .Value
    .Offset(0, -2).Resize(, 3).RemoveDuplicates Columns:=1, Header:=xlNo
End With
[B:B].Delete
End Sub
 
Last edited:
Upvote 0
Try this :
Code:
Sub Test()
Dim rng As Range
[D1:D2] = [A1:A2].Value
[A:C,E:L].Delete
[C4] = "Duration"
Set rng = Range([C5], Cells(Rows.Count, "A").End(xlUp)(1, 3))
With rng
    .NumberFormat = "hh:mm:ss"
    .Formula = "=SUMIF(" & .Offset(0, -2).Address & ",A5," & .Offset(0, -1).Address & ")"
    .Value = .Value
    .Offset(0, -2).Resize(, 3).RemoveDuplicates Columns:=1, Header:=xlNo
End With
[B:B].Delete
End Sub

Thanks for that, it works up to a point. However, it doesn't add up the duration correctly.
 
Upvote 0
Thanks for that, it works up to a point. However, it doesn't add up the duration correctly.

Please provide some sample data that is not being summed properly.
What the format of your Duration column?
 
Upvote 0
Please provide some sample data that is not being summed properly.
What the format of your Duration column?

These are some examples, the format is hh:mm:ss.

DriverName
Duration

Richard Taylor 1335

00:17:17

<colgroup><col><col></colgroup><tbody>
</tbody>

Jason Ashton 1326

18:21:40


<colgroup><col width="301"><col width="182"></colgroup><tbody>
</tbody>
Peter Reed 128714:15:12

<colgroup><col width="301"><col width="182"></colgroup><tbody>
</tbody>
 
Upvote 0
I get the correct totals (e.g. 00:56:46 for Jason Ashton) when I run the macro.

Try stepping though the code via F8 to see what's happening, particularly what formula the macro is putting in column C.

Does your original Duration column contain any formulas ?
 
Upvote 0
I get the correct totals (e.g. 00:56:46 for Jason Ashton) when I run the macro.

Try stepping though the code via F8 to see what's happening, particularly what formula the macro is putting in column C.

Does your original Duration column contain any formulas ?

There are no formulas in the original duration column. This is the formula the macro is using: =SUMIF($A$5:$A$18437,A5,$B$5:$B$18437)
 
Upvote 0
The formula is correct.

In an empty cell on your original sheet try the sum function for one of the drivers. Does it produce the correct total?

Does Jason Ashton only appear on rows 10/11/12 or also on other rows? How is your original data sorted?
 
Upvote 0
The formula is correct.

In an empty cell on your original sheet try the sum function for one of the drivers. Does it produce the correct total?

Does Jason Ashton only appear on rows 10/11/12 or also on other rows? How is your original data sorted?

Yes it does add up correctly, the spreadsheet is 18,500 columns in total. I've hidden some of it for data protection reasons.
 
Upvote 0

Forum statistics

Threads
1,216,178
Messages
6,129,327
Members
449,502
Latest member
TSH8125

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