Solved

Delete records using AutoFilter

Posted on 2016-11-11
6
16 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
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 how to use longer labels with horizontal bar charts instead of the vertical column chart.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now