VBA Delete rows in table that are < Date - 7

JulianvO

New Member
Joined
Sep 9, 2022
Messages
29
Office Version
  1. 2021
Platform
  1. Windows
Hi

Table1 contains 6 columns of which column 6 is in a short date format.

The date in column 6 must be automatically deleted if less than 7 days from the current date or a date in another cell - control cell.
The code below gives error 424 Object required. The code runs from the workbook.

Option Explicit

Sub DeleteRows()

Dim LastRow As Range
Dim i As Integer

Set LastRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row

For i = LastRow To 2 Step -1

If ThisWorkbook.Worksheets("Sheet1").Cells(i, 6).Value < Date Then

Rows(i).Delete

End If
Next

ThisWorkbook.Worksheets("Sheet1").Cells(i, 1).Select

End Sub

Can someone kindly assist me?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
LastRow is a Long not a Range, also should not have the word Set in front of the line because it isn't a Range/Object
 
Last edited:
Upvote 0
Hi Mark858

Thanks for the assistance. Works well, but deletes all the rows above the table, but one. Table is located at A11.

Any further advice?

Thanks again
 
Upvote 0
You could just use autofilter

VBA Code:
Sub jec()
 Application.ScreenUpdating = False
 With Range("A11").CurrentRegion
   .AutoFilter 6, "<" & CLng(Date - 7)
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub

It is even better when you refer to a real tablename

VBA Code:
Sub jecc()
 Application.ScreenUpdating = False
 With Sheets(1).ListObjects("Table1").Range
   .AutoFilter 1, "<" & CLng(Date - 7)
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
 
Last edited:
Upvote 0
Solution
You could just use autofilter

VBA Code:
Sub jec()
 Application.ScreenUpdating = False
 With Range("A11").CurrentRegion
   .AutoFilter 6, "<" & CLng(Date - 7)
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub

It is even better when you refer to a real tablename

VBA Code:
Sub jecc()
 Application.ScreenUpdating = False
 With Sheets(1).ListObjects("Table1").Range
   .AutoFilter 1, "<" & CLng(Date - 7)
   .Offset(1).EntireRow.Delete
   .AutoFilter
 End With
End Sub
Hi JEC

Thanks. Will keep your suggestions.


They will come in handy!
 
Upvote 0
Hi Mark858

Thanks. That did it!
You're welcome, personally I would go with the autofilter method, just with the exception that you don't need the offset if you use DataBodyRange rather than Range

VBA Code:
Sub RemoveDates()
    With Sheets(1).ListObjects("Table1").DataBodyRange
        .AutoFilter 1, "<" & CLng(Date - 7)
        .EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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