VBA: delete all rows where value (time) is less than 1 minute

JofFrey

New Member
Joined
Apr 3, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello, I have to analyse a file where almost every 10 seconds a record is generated which is almost impossible to do. Therefore I would like to keep records where the date & time (Column C Start date) difference does not exceed 1 minute. I also want to keep the records where the speed (Column D) = "0 km/h".
So in the example underneath I would like to keep the records where I have added " good". I have no idea how to do this so basically I am doing it manually. However if one of you Excel wizards could create a a function in VBA that deletes those records or marks them in any way that I could filter them out it would mean a real help. I am talking about 2500-6000 each day so you know what I have to go through and how many coffees that would take.
BTW if I do C8-C2= 0,000694444 (so 1 minute equals 0,000694444).

Start dateSpeed
16/03/2024 07:51:36 good37 km/h
16/03/2024 07:51:4614 km/h
16/03/2024 07:51:5640 km/h
16/03/2024 07:52:0646 km/h
16/03/2024 07:52:1672 km/h
16/03/2024 07:52:2664 km/h
16/03/2024 07:52:36 good37 km/h
16/03/2024 07:52:4672 km/h
16/03/2024 07:52:5664 km/h
16/03/2024 07:53:0759 km/h
16/03/2024 07:53:1644 km/h
16/03/2024 07:53:2714 km/h
16/03/2024 07:53:37 good26 km/h
16/03/2024 07:53:4761 km/h
16/03/2024 07:53:5772 km/h
16/03/2024 07:54:0657 km/h
16/03/2024 07:54:1663 km/h
16/03/2024 07:54:2661 km/h
16/03/2024 07:54:3650 km/h
16/03/2024 07:54:46 good57 km/h
16/03/2024 07:54:5652 km/h
16/03/2024 07:55:0638 km/h
16/03/2024 07:55:1650 km/h
16/03/2024 07:55:2640 km/h
16/03/2024 07:55:3646 km/h
16/03/2024 07:55:46 good50 km/h
16/03/2024 07:55:5661 km/h
16/03/2024 07:56:0670 km/h
16/03/2024 07:56:1661 km/h
16/03/2024 07:56:2753 km/h
16/03/2024 07:56:3631 km/h
16/03/2024 07:56:46 good42 km/h
16/03/2024 07:56:5664 km/h
16/03/2024 07:57:0661 km/h
16/03/2024 07:57:1653 km/h
16/03/2024 07:57:2646 km/h
16/03/2024 07:57:36 good0 km/h
16/03/2024 07:57:46 good0 km/h
16/03/2024 07:57:57 good0 km/h
 
MrExcel_deleteTimDif.xls
ABCD
1Start dateSpeed
216/03/2024 07:51:3637 km/h
316/03/2024 07:51:4614 km/h
416/03/2024 07:51:5640 km/h
516/03/2024 07:52:0646 km/h
616/03/2024 07:52:1672 km/h
716/03/2024 07:52:2664 km/h
816/03/2024 07:52:3637 km/h
916/03/2024 07:52:4672 km/h
1016/03/2024 07:52:5664 km/h
1116/03/2024 07:53:0759 km/h
1216/03/2024 07:53:1644 km/h
1316/03/2024 07:53:2714 km/h
1416/03/2024 07:53:3726 km/h
1516/03/2024 07:53:4761 km/h
1616/03/2024 07:53:5772 km/h
1716/03/2024 07:54:0657 km/h
1816/03/2024 07:54:1663 km/h
1916/03/2024 07:54:2661 km/h
test
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
when I use the above, it still pastes in as Custom. For my previous code just posted, I forced all to be "Text" in General formatting.

So that latest code should work for you.

Rob
 
Upvote 0
If its plain text in your file, maybe this can work :

VBA Code:
Sub keep_good()

Dim lr, x As Long

lr = Cells(Rows.Count, 3).End(xlUp).Row
good_val = TimeValue(Right(Range("c2"), 8))

For x = 3 To lr 'start from row3 as we first value is always "good".
  
    If Range("C" & x) = "" Then Exit For 'check for blank rows to quit macro
   
       new_val = TimeValue(Right(Range("C" & x), 8))
    If new_val - good_val > 0.000694444 Or Left(Range("D" & x).Value, 1) = 0 Then
        good_val = TimeValue(Right(Range("c" & x).Value, 8))
    Else
        Range("C" & x).EntireRow.Delete
        x = x - 1
    End If

Next x

End Sub
Hi Rob, thank you very much, that worked as a charm, just had to declare "good_val" and "new_val" as a variable and perfect!
If you're ever in Belgium feel free to pass by for a cup of coffee, you saved me from drinking liters.
Thanks again!
 
Upvote 0
Great, glad it worked finally - thanks for the feedback.

Yes I plan to be there next year ! so won't forget.. ;)

Rob
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

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