Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete records using AutoFilter

Posted on 2016-11-11
6
Medium Priority
?
55 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 46

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 46

Accepted Solution

by:
aikimark earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 46

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

972 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