RattlingCarp3048
Board Regular
- Joined
- Jan 12, 2022
- Messages
- 202
- Office Version
- 365
- Platform
- Windows
I have googled and searched other platforms with no clear answer. hopefully someone here can help!
Currently we are manually filtering column P to display all rows where the date is older than 5 days back, then deleting the entire rows. In an effort to automate this I have modified a VBA code that loops through to do this. The code works fine for smaller volumes of data but is soooooooo slow with larger data. Is there a way to improve the speed of the loop? Is there another method that may work just as well instead of a loop?
worksheet regularly contains 30,000+ rows. Column P is date/time but formatted to display only the date. the # of rows older than 5 days will regularly be 3,000-5,000 rows.
Sub Delete()
'
' Assign worksheets to worksheet variables
Set ws3 = Sheets("Today's Final Report")
' Find last row with data in column P on Current List
lr = ws3.Cells(Rows.Count, "P").End(xlUp).Row
' Loop through all rows on Today's Final Report backwards, up to row 2
For r = lr To 2 Step -1
' See if date in column P > 5
If Date - ws3.Cells(r, "P").Value > 5 Then
' Delete row on sheet 3
ws3.Rows(r).Delete Shift:=xlUp
End If
Next r
Application.ScreenUpdating = True
End Sub
Currently we are manually filtering column P to display all rows where the date is older than 5 days back, then deleting the entire rows. In an effort to automate this I have modified a VBA code that loops through to do this. The code works fine for smaller volumes of data but is soooooooo slow with larger data. Is there a way to improve the speed of the loop? Is there another method that may work just as well instead of a loop?
worksheet regularly contains 30,000+ rows. Column P is date/time but formatted to display only the date. the # of rows older than 5 days will regularly be 3,000-5,000 rows.
Sub Delete()
'
' Assign worksheets to worksheet variables
Set ws3 = Sheets("Today's Final Report")
' Find last row with data in column P on Current List
lr = ws3.Cells(Rows.Count, "P").End(xlUp).Row
' Loop through all rows on Today's Final Report backwards, up to row 2
For r = lr To 2 Step -1
' See if date in column P > 5
If Date - ws3.Cells(r, "P").Value > 5 Then
' Delete row on sheet 3
ws3.Rows(r).Delete Shift:=xlUp
End If
Next r
Application.ScreenUpdating = True
End Sub