Solved

AutoFilter Delete not keeping Headers?

Posted on 2016-11-21
2
26 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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.

696 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