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!
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,336
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,453
Messages
5,528,876
Members
409,843
Latest member
akostaki
Top