Help with VBA to delete anything lsss the 30mins

pkam2010

New Member
Joined
Mar 29, 2019
Messages
19
HI

i am very new to VBA, I have been working on them but mainly finding them on the net and then pasting and adjusting them to suit. i require help to work out how to delete a row if in column N the time is less than 30mins. Also to note when I paste the data into the excel sheet it is in text format. I have tried a number of codes found on the net and converted the cells to time but doesn’t seem to work.

please help
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to the Board!

Can you post some examples of what these times values being copied down actually look like?
 
Upvote 0
HI

see below

Duration
00:54:34
00:01:33
00:09:49
01:20:37
00:02:57
00:03:22
01:26:18
01:39:14
00:00:58
00:04:37
00:03:36
00:07:57
02:28:45

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>

so i need to be able to delete all the rows less then 30mins.

thanks
 
Upvote 0
Try this:
Code:
Sub MyDeleteRows()

    Dim c As String
    Dim lr As Long
    Dim r As Long
    Dim dt As Date
    
    Application.ScreenUpdating = False
    
'   Designate which column time values are in
    c = "A"
    
'   Find last row in column with data
    lr = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows, starting at the bottom, up to row 2
    For r = lr To 2 Step -1
'       Convert time entry to date/time with current date
        dt = Date + TimeValue(Cells(r, c))
'       Check to see if entry is less than 30 minutes ago
        If (Now() - dt) < (1 / 48) Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
i tired inputting all the data again then running the marco and it is coming with a error of Run-time error '13' type mismatch
 
Upvote 0
It works for me, assuming that your values are really Text.
Note also that all the times in the sample that you posted are over 30 minutes (at least in my time zone), so no rows would be deleted in that example.

If you are copying the data down from the Web, often times other invisible "special" characters (like non-breaking spaces) are copied down, that may be causing the time conversion to fail.
Try adding a message box, and see if it is building the date/time piece conversion successfully. It should return today's date with the time value from the cell.
Code:
Sub MyDeleteRows()

    Dim c As String
    Dim lr As Long
    Dim r As Long
    Dim dt As Date
    
    Application.ScreenUpdating = False
    
'   Designate which column time values are in
    c = "A"
    
'   Find last row in column with data
    lr = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows, starting at the bottom, up to row 2
    For r = lr To 2 Step -1
'       Convert time entry to date/time with current date
        dt = Date + TimeValue(Cells(r, c))
[COLOR=#ff0000]        MsgBox "Value in cell " & Cells(r, c).Address(0, 0) & ": " & Format(dt, "mm/dd/yyyy hh:mm:ss")[/COLOR]
'       Check to see if entry is less than 30 minutes ago
        If (Now() - dt) < (1 / 48) Then
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
the time is 00:00:00 is Hrs:min:Sec and not day:Hrs:mm that where may be going wrong
.
Yep, I think it may be the extra character.
Is your first time entry in cell N2?
Place this formula in any blank cell, and tell me what it returns?
Code:
=LEN(N2)
Then tell me what this formula returns:
Code:
=LEFT(N2,8)
 
Upvote 0
Hi

1st times are in n5 so
=Len(n5) result was 8
=Left(n5) result was 00:54:34
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
</body>
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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