VBA Filter Code Filtering all at once

Minx_Minxy

New Member
Joined
Jul 13, 2015
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hello my fellow Vba coders

My worksheet contains a table where I want it filtered based on two separate dropdown list. The user will first filter cell B3 where the table filters the data accordingly, and then if they choose to, by selecting a value on cell B6, the table filters the information further based on the two values the user has selected.

However, when I run the code the table gets filtered in a way that no value shows up. Also, could there be a way that if the user selects another value on cell B3, the table resets itself, meaning, it un-filters what is currently there and start again. Below is the code I am working with

VBA Code:
Private Sub Worksheet_ChangeDepot(ByVal Target As Range)

If Target.Address = "$B$3" Or Target.Address = "$B$6" Then
    Sheets("Dashboard").ListObjects("EmployeeHistory").Range.AutoFilter Field:=3, Criteria1:=Range("B3").Value
    Sheets("Dashboard").ListObjects("EmployeeHistory").Range.AutoFilter Field:=2, Criteria1:=Range("B6").Value
End If
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Private Sub Worksheet_ChangeDepot(ByVal Target As Range)
I don't know why you have the changedepot event? Is it an event or is it a macro?

If you need the Change event, then try the following:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim tbl As ListObject
  
  Set tbl = Sheets("Dashboard").ListObjects("EmployeeHistory")
  
  If Target.Address = "$B$3" Then
    tbl.Range.AutoFilter
    If Target.Value = "" Then Exit Sub
    tbl.Range.AutoFilter Field:=3, Criteria1:=Target.Value
  End If
  If Target.Address = "$B$6" Then
    tbl.Range.AutoFilter Field:=2, Criteria1:=Target.Value
  End If
End Sub
 
Upvote 0
Solution
I don't know why you have the changedepot event? Is it an event or is it a macro?

If you need the Change event, then try the following:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim tbl As ListObject
 
  Set tbl = Sheets("Dashboard").ListObjects("EmployeeHistory")
 
  If Target.Address = "$B$3" Then
    tbl.Range.AutoFilter
    If Target.Value = "" Then Exit Sub
    tbl.Range.AutoFilter Field:=3, Criteria1:=Target.Value
  End If
  If Target.Address = "$B$6" Then
    tbl.Range.AutoFilter Field:=2, Criteria1:=Target.Value
  End If
End Sub

it is an event, I was trying to get it to work in any way!

But your response worked perfectly! Thank you so much
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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