filtering data through listboxes

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
Office Version
  1. 2016
Platform
  1. Windows
I've been spending already quite some time on this, can't find it:

I have a range of data which needs to be filtered by employee name and project. I select the filter criteria through two listboxes. When I change the selection in either listbox, then the filter results should be displayed in cols A B C D.

Any help is appreciated.
 

Attachments

  • listbox.png
    listbox.png
    83.6 KB · Views: 18

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Assuming the listboxes are listbox1 for the name (column A) and listbox2 for the project (column C).
Which both have the MultiSelect property.
🧙‍♂️

Then try the following code. Put all the code in your userform.

VBA Code:
Private Sub ListBox1_Change()
  Call Proc_Filter_Data
End Sub

Private Sub ListBox2_Change()
  Call Proc_Filter_Data
End Sub

Sub Proc_Filter_Data()
  Dim i As Long, n As Long
  Dim st1 As String, st2 As String
  Dim ar1 As Variant, ar2 As Variant
  
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then st1 = st1 & .List(i) & ","
    Next
  End With
  If st1 <> "" Then
    st1 = Left(st1, Len(st1) - 1)
    ar1 = Split(st1, ",")
    Range("A1:D31").AutoFilter 1, ar1, xlFilterValues
  End If

  With ListBox2
    For i = 0 To .ListCount - 1
      If .Selected(i) Then st2 = st2 & .List(i) & ","
    Next
  End With
  If st2 <> "" Then
    st2 = Left(st2, Len(st2) - 1)
    ar2 = Split(st2, ",")
    Range("A1:D31").AutoFilter 3, ar2, xlFilterValues
  End If
End Sub

Comment on the results.
Respectfully
Dante Amor
 
Upvote 0
Edit:

Use this code:
VBA Code:
Private Sub ListBox1_Change()
  Call Proc_Filter_Data
End Sub

Private Sub ListBox2_Change()
  Call Proc_Filter_Data
End Sub

Sub Proc_Filter_Data()
  Dim i As Long, n As Long
  Dim st1 As String, st2 As String
  Dim ar1 As Variant, ar2 As Variant
  
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then st1 = st1 & .List(i) & ","
    Next
  End With
  If st1 <> "" Then
    st1 = Left(st1, Len(st1) - 1)
    ar1 = Split(st1, ",")
    Range("A1:D1").AutoFilter 1, ar1, xlFilterValues
  End If

  With ListBox2
    For i = 0 To .ListCount - 1
      If .Selected(i) Then st2 = st2 & .List(i) & ","
    Next
  End With
  If st2 <> "" Then
    st2 = Left(st2, Len(st2) - 1)
    ar2 = Split(st2, ",")
    Range("A1:D1").AutoFilter 3, ar2, xlFilterValues
  End If
End Sub

🫡
 
Upvote 0
Solution

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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