VBA to delete rows older than 30days (No Loop)

Almach

New Member
Joined
Mar 1, 2022
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hello,

In VBA I am looking to delete entire row rows based on the age of the date in column AI. I am currently deleting rows older than 31 days, but since it deletes one row at a time it takes a long times since there are thousands of rows to delete each time I update it. I am not the greatest with VBA. Just a basic understanding and most of the codes I write are just Frankensteined together, so having a hard time figuring this out myself.

Instead of looping through the code line by line, I'm thinking it might be possible to set my range dynamically, but not sure how to go about doing that. My data is always organized from oldest to newest so I dont need to search the whole data set for rows to delete. I would just need to delete from row 2 to the last row that is older than 31 days.


This is the delete code I am currently using in my macro, but its too slow to be viable. (I just call it the main VBA so I can easily turn it on and off when running the main code)

VBA Code:
Sub Delete()

    Application.ScreenUpdating = False
    Dim i As Long
    Dim LastRow As Long

    With Sheets("Data")
        LastRow = .Cells(.rows.Count, "AI").End(xlUp).Row

        For i = LastRow To 2 Step -1
            If DateDiff("d", .Range("AI" & i).Value, Date) > 31 Then
                .rows(i).Delete
            End If
        Next i
    End With
    Application.ScreenUpdating = True

End Sub

Thanks for any advice or help!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Maybe this way....UNTESTED
VBA Code:
Sub MM1()
Dim lr As Long
lr = Sheets("Data").Cells(Rows.Count, "AI").End(xlUp).Row
With Sheets("Data").Range("AI2:AI" & lr)
    .AutoFilter Field:=1, Criteria1:=">" & 31, Operator:=xlFilterValues
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
 
Upvote 0
Thank you for the response Michael.

I tried running the VBA and it fails at the AutoFilter line.
Run-time error '1004':
AutoFilter method of Range class failed

I tried manually filtering the data just to see what it does and it doesn't seem to filter the data correctly at all. Even if I tell it to do today only it shows a bunch of older dates. The column is in Date/Time format so I don't know if that's what's causing it to act strange.

I do have a column AI that is date only, but its in text format. If you think it would be better to convert that to a date column and used that. I can do that during the import process for new days.

Thank you again for the help!
 
Upvote 0
can you post a smaple of your data ??
 
Upvote 0
Sorry for the delay, yesterday ended up being a busy day. I have removed all data but the two date columns. Essentially trying to just do a 30 day rolling, so as it pastes a new day, it deletes the old days. Column B I did convert to a date since it looks like the load times from the data base aren't always once a day or everyday. Tried running the VBA off that column instead and i got the same error.

30 Day Rolling.xlsm
AB
1load_datetimeReport_Date
21/25/22 16:111/24/2022
31/26/22 16:101/25/2022
41/27/22 16:121/26/2022
51/29/22 16:091/28/2022
61/30/22 16:091/29/2022
71/31/22 16:101/30/2022
82/1/22 16:141/31/2022
92/2/22 17:002/1/2022
102/3/22 16:132/2/2022
112/4/22 16:142/3/2022
122/5/22 16:112/4/2022
132/6/22 16:122/5/2022
142/7/22 16:122/6/2022
152/8/22 16:252/7/2022
162/9/22 16:142/8/2022
172/10/22 16:212/9/2022
182/11/22 16:162/10/2022
192/12/22 16:242/11/2022
202/13/22 16:152/12/2022
212/14/22 16:172/13/2022
222/15/22 16:272/14/2022
232/17/22 14:412/15/2022
242/17/22 16:502/16/2022
252/18/22 16:172/17/2022
262/19/22 16:162/18/2022
272/20/22 16:162/19/2022
282/21/22 16:382/20/2022
292/22/22 16:342/21/2022
302/23/22 16:182/22/2022
312/24/22 16:342/23/2022
322/25/22 16:202/24/2022
332/26/22 16:162/25/2022
342/27/22 16:172/26/2022
352/28/22 16:312/27/2022
363/2/22 7:042/28/2022
373/2/22 18:243/1/2022
Data
 
Upvote 0
Mike thanks for your help. I actually found another thread with a similar issue to mine. I don't know why I couldn't find it two days ago, but I guess the way I worded the search today worked better. Appreciate you taking time and helping me solve my issue!


I ended up modifying and using this code and just modifying it for my needs.

VBA Code:
Sub DeleteFromDate()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
Cells.AutoFilter Field:=2, Criteria1:=">=" & DateR
ALR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If ALR > 2 Then
    Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("A2:A" & LR).Delete
    Range("A1").Activate
End If
Cells.AutoFilter
MsgBox "Finished deleting rows"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Ok, glad you got the rquired result...(y):cool:
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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