Dynamic Filter using VBA

wildwally

Board Regular
Joined
Jun 7, 2007
Messages
205
Having a little trouble with this, maybe a fresh set of eyes looking at it might help.

I have a sheet that is filtered through the use of a userform. when the user pushes the button the names are filtered according to their team. Everything works great when the list of names are hard coded into the VBA code. Example here:

Code:
ActiveSheet.ListObjects("CAD").Range.AutoFilter Field:=8, Criteria1:=Array( _
        "Harry", "Joe", "Jason", "Jose"), Operator:=xlFilterValues

Due to the team's changing I have created a settings sheet that the manager can assign the names to, which doesn't require it coming back to me to fix or change the hard code. So I'm trying to get this to work and it's not.

Code:
 Dim lst As String
Dim rng As Range

Set rng = Worksheets("Settings").Range("F2:F12")

lst = Empty

For Each cell In rng
    If cell <> "" Then
        If lst = "" Then
            lst = ("""" & cell & """")
        Else
            lst = (lst & ", " & """" & cell & """")
        End If
    Else
    End If

Next

ActiveSheet.ListObjects("CAD").Range.AutoFilter Field:=8, Criteria1:=Array(lst)


Any suggestions?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,509
Messages
6,179,192
Members
452,893
Latest member
denay

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