?
Solved

AutoFilter Delete not keeping Headers?

Posted on 2016-11-21
2
Medium Priority
?
30 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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

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