Re: Autofilter data based on listbox selections
Thanks, actually I had tried earlier before seeking your help, only that my code filters out the ENTIRE data range i.e all the data is invisible. This is so even if selections are made in only one listbox. I am relatively new to VBA (barely 5 months),and I am not very good with arrays, so I hoped for some kind of a loop to loop through all the listboxes.
Anyway, this is what I had:
Code
Private Sub Cmd1_Click()
Dim x, k, r As Variant
ReDim x(0)
Application.ScreenUpdating = False
For i = 0 To ListBox2.ListCount - 1
If Me.ListBox2.Selected(i) Then
x(UBound(x)) = Me.ListBox2.List(i)
ReDim Preserve x(UBound(x) + 1)
End If
Next i
Range("A3:C600").AutoFilter Field:=2, Criteria1:=x, Operator:=xlFilterValues
ReDim r(0)
For j = 0 To ListBox1.ListCount - 1
If Me.ListBox1.Selected(j) Then
r(UBound(r)) = Me.ListBox1.List(j)
ReDim Preserve r(UBound(r) + 1)
End If
Next j
Range("A3:C600").AutoFilter Field:=1, Criteria1:=r, Operator:=xlFilterValues
ReDim k(0)
For s = 0 To ListBox3.ListCount - 1
If Me.ListBox3.Selected(s) Then
k(UBound(k)) = Me.ListBox3.List(s)
ReDim Preserve k(UBound(k) + 1)
End If
Next k
Range("A3:C600").AutoFilter Field:=3, Criteria1:=k, Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub
Display More
Please direct me further. Thanks.