Filter Function in User Form

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi
In my spreadsheet I have a table and have used the filter with the choose column function to filter data based on a name i insert into an adjacent cell.
The question is can this be done in a similar manner within a user form. I already have in my userform a combo box that displays all data in Table1 column x. Is there a way to add a command button to filter all matching cells in table and return all results to populate a list box.
Thanks ever so much for any help.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Yes. I created a generic table and userform as an example. You'll have to replace the sheet names, ranges, form control names, and such


VBA Code:
Private Sub TextBox1_Change()
  Dim Rng As Range
  Dim Ary As Variant
  Dim App As Application
  Dim WF As WorksheetFunction
  Dim TBVal As String
  Dim Sht As Worksheet
  Dim Crit As String
  Dim Cel As Range
  Dim C1 As String
  
  C1 = Chr(1)
  Set Sht = Sheets("Sheet1")
  TBVal = UCase(TextBox1.Value)
  Set Rng = Sht.Range("Table1[Two]")
  If TBVal = "" Then
    Ary = Rng.Value
    ListBox1.List = Ary
  Else
    For Each Cel In Rng
      If InStr(UCase(Cel.Value), TBVal) > 0 Then
        If Len(Crit) > 0 Then
          Crit = Crit & C1 & Cel.Value
        Else
          Crit = Cel.Value
        End If
      End If
    Next Cel
    If Crit <> "" Then
      Ary = Split(Crit, C1)
      ListBox1.List = Ary
    End If
  End If
  

End Sub



Private Sub UserForm_Initialize()
  Dim Rng As Range
  Dim Ary As Variant
  Dim App As Application
  Dim WF As WorksheetFunction

  Set Rng = Sheets("Sheet1").Range("Table1[Two]")
  Ary = Rng.Value
  ListBox1.List = Ary
  
End Sub


Cell Formulas
RangeFormula
B2:B20B2=[@Three]&A2
C2:C20C2=TEXT(DAY(A2+1),"dddd")
 
Upvote 0
I didn't actually create a button for you. The textbox on the form allows you to filter the range by adding text you want to match. That updates the listbox each time a character is added or subtracted.
 
Upvote 0
Gosh, Thanks a million, it works beautifully.

Would it to much effort to advise me on how to edit this code to include more than a single column of data from the table range and to include the results in the list box as multicolumns?

Thanks again
 
Upvote 0

Forum statistics

Threads
1,215,123
Messages
6,123,183
Members
449,090
Latest member
bes000

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