VBA autofilter based on cell contents

Rseq2

New Member
Joined
Sep 15, 2014
Messages
1
Hello,

I'm trying to sort a table based on a drop-down menu in cell F2. The below code I found online works great, except when I change the drop-down to "all" I need it to show all records and it currently filters for text "all". Any thoughts? I feel like this is a quick fix- just can't quite figure it out...

Private Sub Worksheet_Change(ByVal Target As Range)
If (Intersect(Target, Range("F2")) ="All") _
Then
Exit Sub
End If
Cells.AutoFilter Field:=6, Criteria1:="=" & Range("F2")
End Sub

Thanks!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,460
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Possibly...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F2")) = "All" _
       Then
        ActiveSheet.ShowAllData
    Else
        Cells.AutoFilter Field:=6, Criteria1:="=" & Range("F2")
    End If
End Sub

or strictly speaking

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F2")) = "All" _
       Then
        Cells.AutoFilter Field:=6
    Else
        Cells.AutoFilter Field:=6, Criteria1:="=" & Range("F2")
    End If
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,120
Messages
5,835,504
Members
430,362
Latest member
ConnerJ

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