How to remove repeating time intervals?

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
17:58:282020-08-02LocationDim LevelCHANGE
17:57:092020-08-02xOffOn to Off
17:06:452020-08-02xOnOn
16:45:362020-08-02xOffOn to Off
08:15:032020-08-02xOnOn
02:43:522020-08-02xOffOff
02:33:502020-08-02xOffOn to Off
02:05:032020-08-02xOnOn
01:05:022020-08-02xOffOff
00:56:452020-08-02xOffOff
00:28:452020-08-02xOffOff
00:05:022020-08-02xOffOff
23:05:032020-08-01xOffOff
18:09:302020-08-01xOffOff
16:43:462020-08-01xOffOn to Off
08:15:032020-08-01xOnOn
03:49:462020-08-01xOffOff
03:46:092020-08-01xOffOn to Off
02:05:022020-08-01xOnOn
01:05:022020-08-01xOffOff
00:05:032020-08-01xOffOff
23:05:022020-07-31xOffOff
23:00:032020-07-31xOffOff
17:16:502020-07-31xOffOff
17:16:502020-07-31xOffOff


Hi, I have time data and I want to remove reapeating time intervals, at 02:05:03, at 01:05:02 etc. It could also be 23:05:13 et.

The minute is always 05 but the hour and the seconds varies but I still want to remove the corresponding rows.

I also want to delete multiple Off to Off in the change column. If the change column has Off, Off, Off I want to remove the corresponding rows.

If you have any questions, I'll try to answer as best as I can.

Grateful for your assistance!

I am looking for a VBA solution!


02:05:03​
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Not sure what you meant by the "Off" criteria but the code below removes the duplicate minutes and keeps only one (Assuming your times are on column A)
VBA Code:
Sub RemoveMin()
    lr = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = lr To 2 Step -1
        For j = i - 1 To 1 Step -1
            If Minute(Range("A" & j)) = Minute(Range("A" & i)) Then
                Range("A" & j).EntireRow.Delete
                Exit For
            End If
        Next j
    Next i
End Sub
 
Upvote 0
Hi, thank you for your reply!

I have times in column A but I get a type mismatch when I run your code.

I want to delete all the minutes. So 23:05:02, 00:05:03, 01:05:04 all get deleted.

Is this possible?
 
Upvote 0
Maybe there is a better way but I can do it by using a helper column.
I'm assuming column F is blank (if not you can change it to another column)

give this a try

VBA Code:
Sub RemoveMin()
    On Error Resume Next
    lr = Range("A" & Rows.Count).End(xlUp).Row

'Identify duplicates
    For i = 1 To lr - 1
        For j = i + 1 To lr
            If Range("F" & i).Value <> "x" Then
                If Minute(Range("A" & j)) = Minute(Range("A" & i)) Then
                    Range("F" & i).Value = "x"
                    Range("F" & j).Value = "x"
                End If
            End If
        Next j
    Next i
   
'Remove duplicates
    For i = lr To 1 Step -1
        If Range("F" & i).Value = "x" Then
            Range("F" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Upvote 0
Hi again,

thank you for your quick reply and your code.

I did a quick test and of 3000 rows only 60 are left after running your macro.

Is there something I am doing wrong? I want to keep all times that are not xx:05:xx.
 
Upvote 0
Oh my bad, I thought you wanted to remove all the duplicate times.
This make the job even easier.

If you only wanna remove minute "05"s then try this:
VBA Code:
Sub Remove05()
    On Error Resume Next
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If Minute(Range("A" & i)) = 5 Then
            Range("A" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Upvote 0
Hi, thank you again.

Now your code works great!

Maybe you can take a swing on deleting the unwanted off's?
 
Upvote 0
I'm not clear what you need for the Off situation. You will need to elaborate more
 
Upvote 0
Hi,

I'll try to elaborate as good as I can.

Time + 1hTimeDateTime2LocationDim LevelStartEndLengthStatus
21:02:4420:02:4425-08-201920:02:44 25/08/2019xOff0:00Off to Off
18:39:0717:39:0725-08-201917:39:07 25/08/2019xOff18:3921:022:23On to Off
17:51:5916:51:5925-08-201916:51:59 25/08/2019xOn17:5118:390:47On
13:02:0612:02:0625-08-201912:02:06 25/08/2019xOff13:0217:514:49Off to Off
09:23:108:23:1025-08-201908:23:10 25/08/2019xOff9:2313:023:38On to Off
07:20:306:20:3025-08-201906:20:30 25/08/2019xOn7:209:232:02On
03:05:032:05:0325-08-201902:05:03 25/08/2019xOff3:057:204:15Off to Off
02:05:021:05:0225-08-201901:05:02 25/08/2019xOff2:053:051:00Off to Off
01:05:030:05:0325-08-201900:05:03 25/08/2019xOff1:052:050:59Off to Off
00:59:1623:59:1624-08-201923:59:16 24/08/2019xOff0:591:050:05On to Off
00:47:4323:47:4324-08-201923:47:43 24/08/2019xOn0:470:590:11On
00:05:0323:05:0324-08-201923:05:03 24/08/2019xOff0:050:470:42Off to Off
21:00:4720:00:4724-08-201920:00:47 24/08/2019xOff21:000:053:04Off to Off
18:30:3417:30:3424-08-201917:30:34 24/08/2019xOff18:3021:002:30On to Off
17:48:0216:48:0224-08-201916:48:02 24/08/2019xOn17:4818:300:42On
05:02:264:02:2624-08-201904:02:26 24/08/2019xOff5:0217:4812:45Off to Off
03:05:032:05:0324-08-201902:05:03 24/08/2019xOff3:055:021:57Off to Off
02:05:031:05:0324-08-201901:05:03 24/08/2019xOff2:053:051:00Off to Off
01:05:030:05:0324-08-201900:05:03 24/08/2019xOff1:052:051:00Off to Off
00:05:0323:05:0323-08-201923:05:03 23/08/2019xOff0:051:051:00Off to Off
21:05:5120:05:5123-08-201920:05:51 23/08/2019xOff21:050:052:59Off to Off
20:15:2519:15:2523-08-201919:15:25 23/08/2019xOff20:1521:050:50Off to Off
13:04:5912:04:5923-08-201912:04:59 23/08/2019xOff13:0420:157:10Off to Off
09:20:268:20:2623-08-201908:20:26 23/08/2019xOff9:2013:043:44On to Off
07:25:266:25:2623-08-201906:25:26 23/08/2019xOn7:259:201:55On
03:05:032:05:0323-08-201902:05:03 23/08/2019xOff3:057:254:20Off to Off
02:05:031:05:0323-08-201901:05:03 23/08/2019xOff2:053:051:00Off to Off
01:05:030:05:0323-08-201900:05:03 23/08/2019xOff1:052:051:00Off to Off


I want to delete Off to Off, but only if there are more then 1 "Off to Off". Se last column and marked with red color.

Does this make sense?

EDIT: In this table, the xx:05:xx minutes are not removed yet.
 
Upvote 0
You tell me if I got it; change the column "E" in the code to your "Status" column and give it a try and lets see...

VBA Code:
Sub RemoveOff()
    lr = Range("E" & Rows.Count).End(xlUp).Row
    cnt = WorksheetFunction.CountIf(Range("E:E"), "Off to Off")
    If cnt > 1 Then
        For i = lr To 1 Step -1
            If Range("E" & i).Value = "Off to Off" Then
                Range("E" & i).EntireRow.Delete
            End If
        Next i
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,094
Members
453,021
Latest member
Justyna P

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