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