VBA code - Autofilter and Deleting

Mattandy55

New Member
Joined
May 27, 2016
Messages
20
Hi all,

I have the below code:

ActiveCell.AutoFilter Field:=9, Criteria1:="<>"
ActiveSheet.AutoFilter.Range.Offset(6, 0).Rows.SpecialCells(xlCellTypeVisible).Delete

I am using it on multiple worksheets and it seems to work fine on the first two, but when I get to the third sheet I have an issue. What is happening is its filtering properly, my column headers are row 6, and sheet 3 has 4 records. So the code filters to row 11 and beyond, but when the delete aspect of the code kicks in, it leaves row 11 and deletes rows starting at row 12. Any idea why its not deleting the first row when the code runs? I'm a little stumped just because it works and deletes rows correctly on the first 2 sheets it runs on.

Thank you
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi @Mattandy55. Thanks for posting on MrExcel.

ActiveSheet.AutoFilter.Range.Offset(6, 0).Rows.SpecialCells(xlCellTypeVisible).Delete
You have an offset, that's why it doesn't delete from row 7.

-----------------------------------------------​

Try the following:
VBA Code:
Sub deleterows()
  Dim lr As Long
 
  lr = Range("I" & Rows.Count).End(3).Row
  Range("A6:I" & lr).AutoFilter Field:=9, Criteria1:="<>"
  ActiveSheet.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
  ActiveSheet.Range("I6").AutoFilter
End Sub

-----------------------------------------------​

If you want it for all sheets.
VBA Code:
Sub deleterows_sheets()
  Dim lr As Long
  Dim sh As Worksheet
 
  For Each sh In Sheets
    lr = sh.Range("I" & Rows.Count).End(3).Row
    sh.Range("A6:I" & lr).AutoFilter Field:=9, Criteria1:="<>"
    sh.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
    sh.Range("I6").AutoFilter
  Next
End Sub

-----------------------------------------------​
If you want it for some sheets:
VBA Code:
Sub deleterows_some_sheets()
  Dim lr As Long
  Dim sh As Worksheet
 
  For Each sh In Sheets
    Select Case sh.Name
      Case "Sheet1", "Sheet2", "Sheet3", "etc"
        lr = sh.Range("I" & Rows.Count).End(3).Row
        sh.Range("A6:I" & lr).AutoFilter Field:=9, Criteria1:="<>"
        sh.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
        sh.Range("I6").AutoFilter
    End Select
  Next
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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