Autofiltering For Values Deletes All Rows

sushi514

New Member
Joined
Nov 13, 2020
Messages
30
Hey All,

I'm not sure how to adjust the line of code below where it deletes rows that match my criteria when filtering. There are times when I do not have records with "delete" and so all rows on the spreadsheet are deleted.
Any suggestions?

Sub filtertest()

Range("N2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(COUNTIF(C[-11],RC[-11])=2,ISNA(RC[-7])),""delete"","""")"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N" & Range("A" & Rows.Count).End(xlUp).Row)

ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A1:N1").AutoFilter Field:=14, _
Criteria1:=Array("delete"), _
Operator:=xlFilterValues

With ActiveSheet.AutoFilter.Range
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
End With
ActiveSheet.ShowAllData

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
VBA Code:
Sub filtertest()
    Range("N2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(AND(COUNTIF(C[-11],RC[-11])=2,ISNA(RC[-7])),""delete"","""")"
    Range("N2").Select
    Selection.AutoFill Destination:=Range("N2:N" & Range("A" & Rows.Count).End(xlUp).Row)
    ActiveSheet.AutoFilterMode = False
    ActiveSheet.Range("A1:N1").AutoFilter Field:=14, _
    Criteria1:=Array("delete"), _
    Operator:=xlFilterValues
    lr = ActiveSheet.Range("A1").CurrentRegion.Rows.Count
    If lr > 1 Then
        c = ActiveSheet.Range("A1:A" & lr).SpecialCells(xlCellTypeVisible).Count - 1
        If c > 0 Then
            With ActiveSheet.AutoFilter.Range
            .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
            End With
        End If
    End If
    ActiveSheet.ShowAllData
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,213,561
Messages
6,114,312
Members
448,564
Latest member
ED38

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