Delete Visible Rows Filtered Using .SpecialCells Not Working

Firesword

New Member
Joined
Oct 10, 2018
Messages
46
Office Version
  1. 2016
Platform
  1. Windows
Hi All (and Happy New Year)

I have some data on a worksheet that has a date the data was added. To stop the report getting to large I only want to keep the current and last year's data and delete the rest. The code listed below needs to filter the data and delete the visible rows. I'm getting a run error but I cannot work out what's wrong.

Currently all the checks are working and the correct date range is being displayed, but I'm getting a run time error when it trys to delete the rows. I've added some comments to the data in case that helps. Any help would be appreciated.

VBA Code:
Dim lStartDate, lLastRow, lVisibleRow, lEndDate  As Long

    With Sheet12
        If .ListObjects(1).showAutofilter Then .ListObjects(1).AutoFilter.ShowAllData 'checks if there is a filter in place and if so clears the filter
        lStartDate = CDate("01/" & Month(Now()) & "/" & Year(Now()) - 1) 'Gets the first day and month of last year
'lStartDate =44197
        .Range("A1").AutoFilter Field:=16, Criteria1:="<" & lStartDate 'Hide current and last year's data, shows the rest
        lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Gets the total rows of the sheet
'lLastRow = 470399
        lVisibleRow = Application.WorksheetFunction.Subtotal(3, .Range("P:P")) 'Gets the visible rows
'lVisibleRow = 22158

        If (lLastRow > 1) And (lVisibleRow > 1) Then .Range("A2:A" & lLastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'checks to make sure there is data to delete which is working. If there is any data then delete it this is the bit that's not working.
'Not working = .Range("A2:A" & lLastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'Error msg = Run-Time error '1004':- Application-defined or object-defined error

        If .ListObjects(1).showAutofilter Then .ListObjects(1).AutoFilter.ShowAllData 'checks if there is a filter in place and if so clears the filter
    End With

Thanks

Simon
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
start small with 12, not immidately +22.000
you delete the first row with this method (no importance, visible or not)
VBA Code:
     With sheet12.ListObjects(1)
          MyNumberOfRows = 12                                   'number of listrows you calculated with your autofilter or otherwise
          .DataBodyRange.Resize(MyNumberOfRows).Delete
     End With
 
Upvote 0
Solution
start small with 12, not immidately +22.000
you delete the first row with this method (no importance, visible or not)
VBA Code:
     With sheet12.ListObjects(1)
          MyNumberOfRows = 12                                   'number of listrows you calculated with your autofilter or otherwise
          .DataBodyRange.Resize(MyNumberOfRows).Delete
     End With
Thank you for the help and posting so quickly. It's working as it should now :-)
 
Upvote 0

Forum statistics

Threads
1,222,405
Messages
6,165,863
Members
451,988
Latest member
boo203

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top