Solved

AutoFilter Delete not keeping Headers?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
consolidate from multiple folders 5 39
macro for same location path 2 19
Formula 6 46
Excel printing page management 2 20
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 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 simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

910 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

21 Experts available now in Live!

Get 1:1 Help Now