Delete rows before a certain time

SCPbrito

Board Regular
Joined
Aug 1, 2024
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have a data set that i need to sort entry numbers from Column"A" from smallest to largest. Then format column "B" to show date and time and then delete all rows that have a timestamp before 6:50am of the current day. Had some help from Chatgpt but it not deleting the rows.


Rich (BB code):
 ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
        Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Columns("B:B").Select
    Selection.NumberFormat = "[$-en-US]m/d/yy h:mm AM/PM;@"
    
    
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim currentTime As Date
    Dim cutoffTime As Date
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

    ' Find the last row with data in column B
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Get the current date and time
    currentTime = Now
    ' Set the cutoff time for the current day at 6:50 AM
    cutoffTime = DateValue(currentTime) + TimeValue("06:50:00")

    ' Loop from the last row to the first row (bottom-up)
    For i = lastRow To 1 Step -1
        ' Check if the timestamp in column A is before the cutoff time
        If IsDate(ws.Cells(i, 1).Value) Then
            If ws.Cells(i, 1).Value < cutoffTime Then
                ' Delete the row if the condition is met
                ws.Rows(i).Delete
            End If
        End If
    Next i
End Sub
 

Attachments

  • 2024-08-05_10-59-34.png
    2024-08-05_10-59-34.png
    24 KB · Views: 8
I've been wracking my brain all day trying to figure this out. I've tried to isolate the code and just use the for loop to delete the rows and still nothing happens. I'm at a loss for words
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I will take a look in a short while. I am not at a place where I can download files right now.

I noticed your link says "Loading Google Sheets". You are doing this in Excel, and not Google Sheets, right?
 
Upvote 0
correct. I just used my personal Google drive to share the file
 
Upvote 0
Hmmm... Doesn't appear I can download that file anyway. It says "Access Denied - you need access".
I am not going to personally request access from my Google account.
 
Upvote 0
I just ran the code against your data file, and since all the dates were before today, it successfully deleted all of them, like it should.
Is it not doing that for you?

I made some very minor adjustments to the code, to temporarily disable screen updating while it is going through the loop (since you are looping over 10000 rows!), and to pop-up a message box to let you know it is done. This doesn't change any of the logic, just a little housekeeping. Here is the code with those updates:
VBA Code:
Sub MyMacro()

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:= _
        Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("$A$1").CurrentRegion
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Columns("B:B").NumberFormat = "[$-en-US]m/d/yy h:mm AM/PM;@"
    
    
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim currentTime As Date
    Dim cutoffTime As Date
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

    ' Find the last row with data in column B
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Get the current date and time
    currentTime = Now
    ' Set the cutoff time for the current day at 6:50 AM
    cutoffTime = DateValue(currentTime) + TimeValue("06:50:00")

    ' Loop from the last row to the first row (bottom-up)
    Application.ScreenUpdating = False
    For i = lastRow To 1 Step -1
        ' Check if the timestamp in column A is before the cutoff time
        If IsDate(ws.Cells(i, 2).Value) Then
            If ws.Cells(i, 2).Value < cutoffTime Then
                ' Delete the row if the condition is met
                ws.Rows(i).Delete
            End If
        End If
    Next i
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"

End Sub
 
Upvote 0
I just ran the code against the file also and its not deleting any line for me. I get the msg "Macro completed" but all rows are intact I'm not sure what is going on. This is very frustrating.
 

Attachments

  • 2024-08-06_17-53-43.png
    2024-08-06_17-53-43.png
    116.1 KB · Views: 5
Upvote 0
@SCPbrito
You did not answer Joe's question

Note: critical information when you use pictures: Row & Column References and Sheet Name.
Are you sure your sheet is really named "Sheet1" and not something else?

If your sheet really is called "Sheet1" then did you try the macro on the workbook you sent us ?
 
Upvote 0
Yes my sheet is called "Sheet1". I will record a video tomorrow using the macro for more clarity
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
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