Solved

AutoFilter Delete not keeping Headers?

Posted on 2016-11-21
2
12 Views
Last Modified: 2016-11-21
I am using the following code to delete records, problem is that when autofilter returns blank.. for some reason it is deleting the header row too? and leaves the autofilter on instead of off.  Any ideas of edits that will fix this?

Sub DeleteOldRecordsPostSavedCOGIs()
'PostSavedCOGIs  'Delete records older than 90 days.
    Sheets("PostSavedCOGIs").Select
    Rows("1:1").AutoFilter  'turn filter on
        ActiveSheet.Range("A:U").AutoFilter Field:=21, Criteria1:= _
        "<" & DateAdd("d", -90, Date), Operator:=xlAnd
    
    LastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row
    LastColumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    'If the filter returns nothing.
    If Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then 'Save the header row if filter is blank.
        Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
    End If
    Rows("1:1").Select
    Range("B1").Activate
    Selection.AutoFilter  'turn filter off
    Range("A2").Select
    
End Sub

Open in new window

0
Comment
Question by:RWayneH
2 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41895993
Hi,

pls try
Sub DeleteOldRecordsPostSavedCOGIs()
'PostSavedCOGIs  'Delete records older than 90 days.
    Sheets("PostSavedCOGIs").Select
    Rows("1:1").AutoFilter  'turn filter on
        ActiveSheet.Range("A:U").AutoFilter Field:=21, Criteria1:= _
        "<" & DateAdd("d", -90, Date), Operator:=xlAnd
    
    LastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row
    If LastRow > 1 Then
        LastColumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
        'If the filter returns nothing.
        If Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then 'Save the header row if filter is blank.
            Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
        End If
        Rows("1:1").Select
        Range("B1").Activate
    End If
    Selection.AutoFilter  'turn filter off
    Range("A2").Select
    
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:RWayneH
ID: 41896014
Working nicely, thanks for the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

757 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

24 Experts available now in Live!

Get 1:1 Help Now