VBA Filter and Delete

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I currently filter my data and then use this bit of code to remove the filtered rows, whilst keeping the header:
Rich (BB code):
With ActiveSheet
    .UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    .AutoFilterMode = False
End With
However, I'm now trying to change this to (ignore the defined range, purely for test purposes):
Rich (BB code):
With Range("A1:C5")
    .AutoFilter
    .AutoFilter field:=2, Criteria1:="1"
    .SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count - 1).Rows.Delete
End With
Line in red is where it fails, I believe my Resize use is not correct, but unsure of what correct sytnax here should be.

Can anyone correct or suggest what to use in combination with SpecialCells(xlCellTypeVisible) so that the header is kept but the remainder of the visible rows are deleted?

Thanks,
Jack
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try
Code:
.SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count - 1).[B][COLOR="Red"]EntireRow[/COLOR][/B].Delete
 
Upvote 0
Thank you Sektor but still receive error message on your version of:

Run-time error '1004':
Application-defined or object-defined error

Which is the same error message I get with my version

I think it's something I'm doing wrong with Resize but can't work out what..
 
Upvote 0
I guess the problem with ".Autofilter" line. Try this.
Code:
Sub G()
    
    With Range("A1:C5")
        [COLOR="Red"]If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.Range.AutoFilter[/COLOR]
        .AutoFilter field:=2, Criteria1:="1"
        .SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count - 1).Rows.Delete
        .AutoFilter
    End With

End Sub
 
Upvote 0
Same error message.

I got this to work:
Code:
i = Range("A" & Rows.Count).End(xlUp).Row
With Range("A1:C" & i)
  .AutoFilter
  .AutoFilter field:=2, Criteria1:="1"
  .Offset(1, 0).SpecialCells(xlCellTypeVisible).Rows.Delete
End With
Only issue is it deletes Row(i+1) but since that row has nothing in it, guess it'll do
 
Upvote 0
The issue was with Range("A1:C5"). It should be Range("A1:C1").
Code:
Sub G()
    
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With Range("A1:C1")
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.Range.AutoFilter
        .AutoFilter field:=2, Criteria1:="1"
        Range("A2:C" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,752
Members
452,940
Latest member
rootytrip

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