VBA - Delete All Rows Where Any Cell In Column E does not Equal Yesterdays Date

PatrickW1907

New Member
Joined
Sep 3, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

I'm hoping someone can help.

I have over 10k rows of data.

A list of dates in column E (uk format i.e. dd/mm/yyyy)

I want to delete any row (except for header) that does not have yesterdays date. I have tried doing Date-1 and then I have tried to get a cell value after i put a formula to get yesterdays date then copied and pasted values into AI1. As I have over 10K data I am trying to do the quickest way possible as For Loops and IFs take an age to run.

The closest I get is the following code

VBA Code:
Sub Test6()

Worksheets("Sheet1").Activate

Range("AI1").Formula = "=TODAY()-1"

Range("AI1").Copy
Range("AI1").PasteSpecial xlPasteValues

With ActiveSheet
    .Range("A:AH").AutoFilter
    .AutoFilter Field:=5, Criterial:=Range("<>AI1").Value
   
    .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter
End With


 
End Sub

At the moment I get the error runtime error 1004 : Method range of object - 'Global' failed but I also have had named object error.

Any help is appreciated.
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

Does this work for you?

VBA Code:
Sub Test7()
  With Worksheets("Sheet1").Range("A:AH")
      .AutoFilter Field:=5, Criteria1:="<>" & CLng(Date - 1)
      .Resize(Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      .AutoFilter
  End With
End Sub
 
Upvote 0
Solution
With Worksheets("Sheet1").Range("A:AH") .AutoFilter Field:=5, Criteria1:="<>" & CLng(Date - 1) .Resize(Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete .AutoFilter End With
Hi Peter

Thanks for this. This is exactly what I am looking for :)

Apologies for the tags ! I didn't know they were there lol. Know for next time. Thanks again !
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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