VBA - Delete Duplicate Rows - Except First and Last

Smerdyakov007

New Member
Joined
Mar 17, 2023
Messages
3
Platform
  1. Windows
Hello guys,

I need your help in writing a macro code to create a time in/time out report. My requirement would be to delete the duplicate rows (based on the 'ID' column) except the first row, but the 'Time Out' column should be having the value of the last duplicate row.

Below is how the report would be looking firstly:

Date​
Time In​
Time Out​
ID​
01-07-2016​
13:30​
13:35​
1223​
01-07-2016​
14:30​
14:35​
1223​
01-07-2016​
15:30​
15:35​
1223​
01-07-2016​
16:30​
16:35​
1224​
01-07-2016​
17:30​
17:35​
1224​
01-07-2016​
18:30​
18:35​
1224​
01-07-2016​
19:30​
19:35​
1224​
01-07-2016​
20:30​
20:35​
1224​
01-07-2016​
21:30​
21:35​
1224​
01-07-2016​
22:30​
22:35​
1225​
01-07-2016​
23:30​
23:35​
1225​
01-07-2016​
00:30​
00:35​
1225​
01-07-2016​
01:30​
01:35​
1225​
01-07-2016​
02:30​
02:35​
1225​
01-07-2016​
03:30​
03:35​
1225​


The final output should be looking like below:

Date​
Time In​
Time Out​
ID​
01-07-2016​
13:30​
15:35​
1223​
01-07-2016​
16:30​
21:35​
1224​
01-07-2016​
22:30​
03:35​
1225​

Is this possible???
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try:
VBA Code:
Sub DeleteDups()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, fVisRow As Long, lVisRow As Long, lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("D2:D" & lRow).Value
    With CreateObject("scripting.dictionary")
        For i = UBound(v) To LBound(v) Step -1
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Range("A1").AutoFilter 4, v(i, 1)
                If [subtotal(103,A:A)] - 1 > 1 Then
                    fVisRow = Rows("2:" & lRow).SpecialCells(xlCellTypeVisible).Row
                    lVisRow = Cells(Rows.Count, "A").End(xlUp).Row
                    Range("C" & fVisRow) = Range("C" & lVisRow)
                    Rows(fVisRow + 1 & ":" & lVisRow).Delete
                End If
            End If
        Next i
    End With
    Range("A1").AutoFilter
End Sub
 
Upvote 2
Solution
Try:
VBA Code:
Sub DeleteDups()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, fVisRow As Long, lVisRow As Long, lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("D2:D" & lRow).Value
    With CreateObject("scripting.dictionary")
        For i = UBound(v) To LBound(v) Step -1
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Range("A1").AutoFilter 4, v(i, 1)
                If [subtotal(103,A:A)] - 1 > 1 Then
                    fVisRow = Rows("2:" & lRow).SpecialCells(xlCellTypeVisible).Row
                    lVisRow = Cells(Rows.Count, "A").End(xlUp).Row
                    Range("C" & fVisRow) = Range("C" & lVisRow)
                    Rows(fVisRow + 1 & ":" & lVisRow).Delete
                End If
            End If
        Next i
    End With
    Range("A1").AutoFilter
End Sub

Thank you so much. This worked 😊
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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