Solved

Delete records using AutoFilter

Posted on 2016-11-11
6
34 Views
Last Modified: 2016-11-11
I am using the following code to delete some records from a sheet.  How would I tell the filter to filter on anything that is older than 90 days? (in Ln7 and 8)  Please advise and thanks.

Sub DeleteOldRecords()
'PostSavedCOGIs  'Delete records older than 90 days.

    Sheets("PostSavedCOGIs").Select
    Rows("1:1").AutoFilter
    
        ActiveSheet.Range("A:U").AutoFilter Field:=21, Criteria1:= _
        "<11/11/2016", Operator:=xlAnd
    
    LastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row
    LastColumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
    
    Rows("1:1").Select
    Range("B1").Activate
    Selection.AutoFilter
    
    
End Sub

Open in new window

0
Comment
Question by:RWayneH
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41883839
ActiveSheet.Range("A:U").AutoFilter Field:=21, Criteria1:= _
        "<" & dateadd("d",-7,date), Operator:=xlAnd

Open in new window

0
 

Author Comment

by:RWayneH
ID: 41883846
Would you mind explaining the code?   Where does it say 90 days?  I would like to use this in other places that the days older than 90 will chg to 60 or even 30..   Not understanding the -7?
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 41883850
Oops.  I posted a seven day solution.  Please try this:
ActiveSheet.Range("A:U").AutoFilter Field:=21, Criteria1:= _
        "<" & dateadd("d",-90,date), Operator:=xlAnd

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:RWayneH
ID: 41883912
Sorry I probably should have mentioned that the filter can be blank...  when it is blank it deletes the header row!!  Is there a way to alter the code to have it not delete that header row (Row1) if the filter returns nothing?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41883923
if nothing is showing but the header, then look at the number of visible rows (count) before you do the delete.  It is a conditional deletion.
if Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).rows.count <>0 then
    'do the delete here
end if

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 41883947
Thanks for the help.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question