Solved

AutoFilter Delete not keeping Headers?

Posted on 2016-11-21
2
16 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 49

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extract Names Based on Position in the Column 12 29
hi all how do i achive this formula in condtional formatting excel 6 18
Excel VBA 4 26
Merging spreadsheets 8 37
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

770 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