VBA Autofilter IF the criteria does not existing

Alexsay

New Member
Joined
Feb 2, 2024
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I tried to put an autofilter in my VBA code but dunno how to put de IF statement if the criteria does not exist
If the criteria does not exist, I just want the code to proceed forward.

Some info about my file
My workbook is a table format
I have a column for checking with Yes or Not and I want to autofilter "Not" and delete the cells, and its work, but when I do not have "Not" and only "Yes", the autofilter is happened (its activated but disapear all data) and broke the code

This is my autofilter and detele code

Set Rng = range("Table1")
Rng.AutoFilter Field:=54, Criteria1:="NU"
range("Table1[#Headers]").Select
Rng.SpecialCells(xlCellTypeVisible).Select
Rng.SpecialCells(xlCellTypeVisible).Delete

Also, if somebody can tell me how can I skip (or automatically press "OK") on the delete message box
1707479063399.png


Any answer it will be more than useful
Thank you
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Give this a try on a copy of your workbook:

VBA Code:
Sub DeleteFilteredRows()
    Dim lo As ListObject
    Dim rngDel As Range
    
    Set lo = Range("Table1").ListObject
    
    With lo
        If .ShowAutoFilter = True Then
            .AutoFilter.ShowAllData
        End If
        .Range.AutoFilter Field:=54, Criteria1:="NU"
        
        If .Range.SpecialCells(xlCellTypeVisible).Address <> .HeaderRowRange.Address Then
            Set rngDel = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            .AutoFilter.ShowAllData
            rngDel.Delete
        End If
    
    End With

End Sub
 
Upvote 0
The previous code I gave you attempts to only delete the rows from the table this is useful if you have data to the right of the table.
It will however fail if you have a table below the table you are working on that has more columns than this table.
If that is the case you will need to delete the entire row.
The code below will do that:

VBA Code:
Sub DeleteFilteredENTIRERows()

    Dim lo As ListObject
    Dim rngDel As Range
  
    Set lo = Range("Table1").ListObject
  
    With lo
        If .ShowAutoFilter = True Then
            .AutoFilter.ShowAllData
        End If
        .Range.AutoFilter Field:=54, Criteria1:="NU"
      
        If .Range.SpecialCells(xlCellTypeVisible).Address <> .HeaderRowRange.Address Then
            Set rngDel = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            Application.DisplayAlerts = False
            rngDel.Rows.Delete
            Application.DisplayAlerts = True
        End If
        .AutoFilter.ShowAllData
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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