Autofilter to exit

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,057
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
the code works all perfect, but need to modify only on the condition if any of the Criteria is not matching.

means if AutoFilter Field:=cfind.Column, Criteria1:="Rejected" or .AutoFilter Field:=ccfind.Column, Criteria1:="" is not matching then it should exit auto filter

VBA Code:
Sub autofiltering()
    Dim col As String, cfind As Range
    Dim coll As String, ccfind As Range

Sheets("Candidate Master").Select

    col = "Cand. Final Status"
    coll = "Email Flag"
    With Worksheets("Candidate Master") '<-- reference your relevant worksheet (change "AF" to your actual worksheet name)
        With .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft)) '<-- reference its row 1 cells from column 1 rightwards to last not empty one
            'Set cfind = .Find(what:=col, LookIn:=xlValues, lookat:=xlWhole) '<-- look for the wanted column header
            Set cfind = .Rows(1).Find(what:=col, LookIn:=xlValues, lookat:=xlWhole)
            Set ccfind = .Rows(1).Find(what:=coll, LookIn:=xlValues, lookat:=xlWhole)
            If Not cfind Is Nothing Then '<-- if the header has been found
                .AutoFilter Field:=cfind.Column, Criteria1:="Rejected" 
                .AutoFilter Field:=ccfind.Column, Criteria1:=""
                ' do your things
            End If
        End With
        '.AutoFilterMode = False '<-- show all rows back and remove autofilter buttons
    End With
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
when you say exit autofilter do you mean just exit and leave filter in place or clear filters all together
 
Upvote 0
after your lines
Excel Formula:
.AutoFilter Field:=cfind.Column, Criteria1:="Rejected"
.AutoFilter Field:=ccfind.Column, Criteria1:=""

try placing this
Excel Formula:
If .SpecialCells(xlCellTypeVisible).Cells.count > 1 Then Cells.AutoFilter
 
Upvote 0
Solution

Forum statistics

Threads
1,214,835
Messages
6,121,880
Members
449,057
Latest member
Moo4247

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