Filter a Range of Data with a Command Button based on a Multi Select ListBox VBA

Heatherisace

New Member
Joined
Jan 31, 2014
Messages
2
Hello,

I am trying to filter a range of data based on the values a user may select from a Multi Select Listbox, but I cannot get it to work.

I have a working Listbox (Listbox1)
My command button (CommandButton1) does not execute the filter, I have searched and searched, and tried many different codes, but I just can't seem to figure it out.

Here is an example of my Table:

OpenOpenClosedClosed
AreaRegionTerritoryProduct XProduct YProduct XProduct Y
A1Joe1638
A1Jan2415
A2Jim4624
B3Jack1512
B4Jill3134
B4Jon2573
C5Jeff1254
C6Judy5331

<tbody>
</tbody>

So if this were my table, say on a sheet called "Master", I would want my filter to start in Row 2, and the values in the listbox are values in column A (A, B, and C in this example) of which any combination of could be selected.

Please help, I am stuck :)
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi welcome to the board.

A simple way to start is turn the macro recorder on & then do your filter. As an example you would get:

Code:
Sub Macro1()
    Selection.AutoFilter Field:=1, Criteria1:="A"
    Selection.AutoFilter Field:=3, Criteria1:="Jim"
End Sub

You can then use this to develop your project further.
If still in need of assistance, post all code have - many here to give support & guidance.

Dave
 
Upvote 0
Hi,

Thank you for your response. I cannot use the recorder because it does not recognize a selection checked in the listbox.

Here is what I have so far. The 2 problems with it are that it does not recognize multiple listbox selections, and that it filters out the header line, so in the above example, if I tried to filter column A for Area "B", it would also remove Row 2 (the header row).

Code:
Private Sub CommandButton1_Click()


    Application.ScreenUpdating = False
    Dim dict As Object, i
    Set dict = CreateObject("Scripting.Dictionary")
    dict.RemoveAll
    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) = True Then dict.Add Me.ListBox1.List(i), ""
    Next
    Const delim = "^^^"
    Dim arr
    If UBound(dict.keys) = -1 Then
        Rows("1:" & Rows.Count).EntireRow.Hidden = False
    Else
        arr = Join(dict.keys, delim)
        arr = arr & delim
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Rows(i).Hidden = Not InStr(arr, Cells(i, 1) & delim) > 0
        Next i
    End If
    Application.ScreenUpdating = True
End Sub
Any additional help someone could give would be much appreciated.

Thanks,
Heather
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,176
Members
449,491
Latest member
maxim_sivakon

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