Solved

Delete records using AutoFilter

Posted on 2016-11-11
6
27 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

823 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