Filtering column for input in ListBox

Kristina96

New Member
Joined
Sep 30, 2019
Messages
33
Hello everyone.

I have a document with a userform that allows users to select a specific area in a list box. Multiple selections are possible.
Based on the input I would like to filter column E called "AreaColumn" for the input made by a user when cofirming the user form with OK.

So far I've tried something like this:
VBA Code:
Private Sub OKButton_Click()
ActiveSheet.Range("AreaColumn").AutoFilter Field:=1, Criteria1:=ListBox1.Value
End Sub

Unfortunately it does not bring the desired result but simply doesn't change the situation.
Could you help me with that?

Thank you in advance and best regards,
Kristina
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
How about
VBA Code:
Private Sub OKButton_Click()
   Dim Ary As Variant
   Dim i As Long, j As Long
   
   With Me.ListBox1
      ReDim Ary(1 To .ListCount)
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            j = j + 1
            Ary(j) = .List(i)
         End If
      Next i
   End With
   ActiveSheet.Range("AreaColumn").AutoFilter 1, Ary, xlFilterValues
End Sub
 
Upvote 0
Thank you very much! It works perfeclty!
How do I have to edit the code to always include the option "everywhere" in the filter no matter what the input is?
 
Upvote 0
Is "everywhere" a value in the "AreaColumn"?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub OKButton_Click()
   Dim Ary As Variant
   Dim i As Long, j As Long
   
   With Me.ListBox1
      ReDim Ary(1 To .ListCount + 1)
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            j = j + 1
            Ary(j) = .List(i)
         End If
      Next i
      Ary(j + 1) = "Everywhere"
   End With
   ActiveSheet.Range("AreaColumn").AutoFilter 1, Ary, xlFilterValues
End Sub
 
Upvote 0
Works, perfectly! Thank you!
If the area selected. is eg. "Germany(Europe)", the filter is also supposed to include "Everywhere (Europe)". Is that possible? To include further criteria based on the input with an if-condition? I need the condition for various inputs.

Thank you very much for your support!
 
Upvote 0
What are all the possible criteria & need results based on the criteria?
 
Upvote 0
the criteria is in the list box.
for example:
List Box1 includes Germany(Europe), France(Europe), China(Asia) and Brazil (Latin America)
those are also the different cells in the column AreaColumn
If I select Germany(Europe) in the listbox I want the macro to filter the AreaColumn for "Germany(Europe)", "All" and "All(Europe)".
Same goes for all other possible selections.

Does that make any sense?
Thank you in advance
 
Upvote 0
Lastly, I don't want to filter at all in case the user doesn't select any option. That would probably be another if-condition?
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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