Removing duplicate time cells/rows from large data tables

simonbevan

New Member
Joined
Jan 26, 2018
Messages
5
Hello,

I wonder if anyone can help. I need to remove multiple cell/row data based on UTC time excluding seconds (ss). By example, I would like to auto remove all rows except A-M, T, V, X-Z in the below table. ID N-O-P-S are all minute 20 data but I only need to keep the first entry of any minute data for further analysis. So the cells/rows that need auto deleting are: O,P,S & U. Some of my data files are 3000 rows in length and I am trying to find a quick means of filtering out data I do not need for further analysis.

IDNameTimestampUTCReportOwnershipSpeedCourseLatitudeLongitude
ANPAC31/12/2017 23:59Auto0xA013.57826.9397733166.601905
BNPAC31/12/2017 23:29Auto0xA0138026.9178233166.4819367
CNPAC31/12/2017 23:21Auto0xA0137926.9127133166.4528817
DNPAC31/12/2017 22:52Auto0xA0137626.8884783166.33775
ENPAC31/12/2017 22:16Auto0xA0137426.8554767166.199985
FNPAC31/12/2017 22:15Auto0xA0137226.8545967166.196685
GNPAC31/12/2017 21:41Auto0xA012.47326.8203483166.0682167
HNPAC31/12/2017 21:38Auto0xA0137726.817525166.0569183
MNPAC31/12/2017 21:21Auto0xA0137226.7993217165.989415
NNPAC31/12/2017 21:20Auto0xA012.47126.7991417165.9886467
ONPAC31/12/2017 21:20Auto0xA012.47126.7991417165.9886467
PNPAC31/12/2017 21:20Auto0xA012.47526.79898165.988075
SNPAC31/12/2017 21:20Auto0xA0137226.79834165.9856367
TNPAC31/12/2017 20:19Auto0xA012.47526.798125165.9848717
UNPAC31/12/2017 20:19Auto0xA012.47526.798125165.9848717
VNPAC31/12/2017 19:19Auto0xA0137026.7978467165.9837333
XNPAC31/12/2017 18:19Auto0xA0137026.7978467165.9837333
YNPAC31/12/2017 17:18Auto0xA0137226.7962217165.9779233
ZNPAC31/12/2017 16:17Auto0xA0137526.796005165.9772283

<colgroup><col span="2"><col><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>

I trust this makes sense!
Simon
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If you can use VBA, i would maybe just loop through each cell and keep track of each time in a vba collection. So Each time you add to the collection, you check to see if the time exists in the collection, if it exists you delete the row and repeat that iteration of the loop so it doesnt skip the next row.

This page explains how to use collections...

http://codevba.com/help/collection.htm#.WmtmKa6nFhE
 
Upvote 0
This is the basic idea (untested, i wrote in comment box, may have errors)

Code:
Sub CleanupTable()
    Dim row As Long
    Dim times As New Collection
    Dim currentDate As Date, d As Date

    row = 2

    Do While Not IsEmpty(Cells(row, 3))
        currentDate = Cells(row, 3)

        For Each d In times
            If d = currentDate Then
                Rows(row).Delete
                GoTo NextLoop
            End If
        Next

        times.Add(currentDate)
        row = row + 1
NextLoop:
    Loop
End Sub
 
Last edited:
Upvote 0
Thanks for this fast response but it does not work. I think the issue remains with the way that excel handles numbers - even as dates with times - as I got multiple erros, e.g. Compile error - for each control variable must be Variant or object or Can't execute code. Any ideas as I have no knowledge on this stuff?

Simon





This is the basic idea (untested, i wrote in comment box, may have errors)

Code:
Sub CleanupTable()
    Dim row As Long
    Dim times As New Collection
    Dim currentDate As Date, d As Date

    row = 2

    Do While Not IsEmpty(Cells(row, 3))
        currentDate = Cells(row, 3)

        For Each d In times
            If d = currentDate Then
                Rows(row).Delete
                GoTo NextLoop
            End If
        Next

        times.Add(currentDate)
        row = row + 1
NextLoop:
    Loop
End Sub
 
Upvote 0
Hi Cerfani

I am a complete beginner with VBA and have not been able to run this code after spending many hours going through various tutorials. I would be grateful if you could test the routine and let me know what I need to change? Many thnaks



This is the basic idea (untested, i wrote in comment box, may have errors)

Code:
Sub CleanupTable()
    Dim row As Long
    Dim times As New Collection
    Dim currentDate As Date, d As Date

    row = 2

    Do While Not IsEmpty(Cells(row, 3))
        currentDate = Cells(row, 3)

        For Each d In times
            If d = currentDate Then
                Rows(row).Delete
                GoTo NextLoop
            End If
        Next

        times.Add(currentDate)
        row = row + 1
NextLoop:
    Loop
End Sub
 
Upvote 0
If all your date/times are 00 seconds as your data posted pastes in Excel then all you need to change is
Code:
Dim currentDate As Date, d As Date
to
Code:
Dim currentDate As Date, d
 
Upvote 0
If your actual data does contain different seconds (not like your data pastes) then try...

Code:
Sub CleanupTable()
    Dim row As Long
    Dim times As New Collection
    Dim currentDate As Date, d

    row = 2

    Do While Not IsEmpty(Cells(row, 3))
        currentDate = Cells(row, 3)

        For Each d In times
            If DateAdd("s", -Second(d), d) = DateAdd("s", -Second(currentDate), currentDate) Then
                Rows(row).Delete
                GoTo NextLoop
            End If
        Next

        times.Add (currentDate)
        row = row + 1
NextLoop:
    Loop
End Sub
 
Upvote 0
Many Thanks - this worked - brilliant! Best regards, Simon




If your actual data does contain different seconds (not like your data pastes) then try...

Code:
Sub CleanupTable()
    Dim row As Long
    Dim times As New Collection
    Dim currentDate As Date, d

    row = 2

    Do While Not IsEmpty(Cells(row, 3))
        currentDate = Cells(row, 3)

        For Each d In times
            If DateAdd("s", -Second(d), d) = DateAdd("s", -Second(currentDate), currentDate) Then
                Rows(row).Delete
                GoTo NextLoop
            End If
        Next

        times.Add (currentDate)
        row = row + 1
NextLoop:
    Loop
End Sub
 
Upvote 0
I got here late but it seems you got it sorted. Thanks Mark for the tip about the time. I overlooked that part.
 
Last edited:
Upvote 0
Hello Cefani and MARKS858

I have taken further look at the data and see that it has not done the changes for all cells. The top table is before and the bottom table is after - you can see that 18:08 and 18:07 still have more than one row? I cant work out why this is happening so I would be grateful for further help?

TimestampUTC
02/07/2017 22:26:08
02/07/2017 21:26:37
02/07/2017 21:17:58
02/07/2017 20:54:00
02/07/2017 20:11:18
02/07/2017 19:35:48
02/07/2017 19:02:28
02/07/2017 18:56:46
02/07/2017 18:55:07
02/07/2017 18:55:06
02/07/2017 18:09:17
02/07/2017 18:08:56
02/07/2017 18:08:26
02/07/2017 18:08:17
02/07/2017 18:08:07
02/07/2017 18:07:56
02/07/2017 18:07:47
02/07/2017 18:07:38
02/07/2017 18:07:26
02/07/2017 18:07:07

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




TimestampUTC
02/07/2017 22:26:08
02/07/2017 21:26:37
02/07/2017 21:17:58
02/07/2017 20:54:00
02/07/2017 20:11:18
02/07/2017 19:35:48
02/07/2017 19:02:28
02/07/2017 18:56:46
02/07/2017 18:55:07
02/07/2017 18:09:17
02/07/2017 18:08:56
02/07/2017 18:08:17
02/07/2017 18:07:56
02/07/2017 18:07:47

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

Many thanks

SimonBevan




I got here late but it seems you got it sorted. Thanks Mark for the tip about the time. I overlooked that part.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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