Autofiltering For Values Deletes All Rows

sushi514

New Member
Joined
Nov 13, 2020
Messages
24
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
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
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,129,814
Messages
5,638,492
Members
417,028
Latest member
JFCLUK

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
Top