Filter listbox

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi all

In a previous inquiry I was shown how to create a user form with a dynamic filter, to populate a list box based on data entered into a separate textbox.

I need though for the list box to be multicolumn, displaying the data from multiple columns in the source table, not just the cells that match the data in the textbox.

I cant seem to get my head around the code though, and if anyone out there is able to help me, it would be greatly appreciated.
please find link of current user form attached.

 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Vba Filter function with Listbox
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
How about
VBA Code:
Private Sub TextBox1_Change()
   Dim Ary As Variant, Rws As Variant
   Dim TBVal As String
   Dim Ws As Worksheet
   
   
   Set Ws = Sheets("Sheet1")
   TBVal = TextBox1.Value
   With Ws.ListObjects("Table1").DataBodyRange
      If TBVal = "" Then
         Ary = .Value
      Else
         Rws = Filter(Evaluate(Replace("transpose(if(isnumber(search(""" & TBVal & """,@)),row(@)-min(row(@))+1,""X""))", "@", .Columns(2).Address)), "X", False)
         If UBound(Rws) < 0 Then
            Me.ListBox1.List = Array("No matches")
            Exit Sub
         ElseIf UBound(Rws) = 0 Then
            Ary = .Rows(Rws(0)).Value
         Else
            Ary = Application.Index(.Value, Application.Transpose(Rws), Evaluate("column(" & .Address & ")"))
         End If
      End If
   End With
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub

Private Sub UserForm_Initialize()
   Dim Ary As Variant
   
   Ary = Sheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub TextBox1_Change()
   Dim Ary As Variant, Rws As Variant
   Dim TBVal As String
   Dim Ws As Worksheet
  
  
   Set Ws = Sheets("Sheet1")
   TBVal = TextBox1.Value
   With Ws.ListObjects("Table1").DataBodyRange
      If TBVal = "" Then
         Ary = .Value
      Else
         Rws = Filter(Evaluate(Replace("transpose(if(isnumber(search(""" & TBVal & """,@)),row(@)-min(row(@))+1,""X""))", "@", .Columns(2).Address)), "X", False)
         If UBound(Rws) < 0 Then
            Me.ListBox1.List = Array("No matches")
            Exit Sub
         ElseIf UBound(Rws) = 0 Then
            Ary = .Rows(Rws(0)).Value
         Else
            Ary = Application.Index(.Value, Application.Transpose(Rws), Evaluate("column(" & .Address & ")"))
         End If
      End If
   End With
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub

Private Sub UserForm_Initialize()
   Dim Ary As Variant
  
   Ary = Sheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub
It works like a charm. Thanks a million.

If its not to much to ask, might I request an adaption?

The spreadsheet I attached in my original query was a sample, [3 col of 20 rows], My spreadsheet that i actually need this user form for is a much larger table. I assumed that I would be able to adapt an answer to suit my spreadsheet, however after trying it seems a little beyond me.
My table actually consists of 15 columns. [A-O]. Column number 14 needs to serve as the source data for the textbox, and must appear in the first column of the list box. Adjacent, i need the list box to display the data from columns 5, 7, 2, 3, 4 respectively [in that particular order].
As I wrote, I assumed I would be able to adapt an answer to my particular needs, however i haven't managed. If you have the time and its not to difficult, it would be a major help if you would be able to tweek your above answer for my specific table.

Thanks again.
 
Upvote 0
Could you ever have only one row that meats the criteria?
 
Upvote 0
Could you have only one row of data that matches what you key into the textbox?
 
Upvote 0
Oh I understand. Yes I could only have 1 matching row. Quite often actually.
Thanks again for your effort
 
Upvote 0
Ok, how about
VBA Code:
Private Sub TextBox1_Change()
   Dim Ary As Variant, Rws As Variant
   Dim TBVal As String
   Dim Ws As Worksheet
   
   
   Set Ws = Sheets("Sheet1")
   TBVal = TextBox1.Value
   With Ws.ListObjects("Table1").DataBodyRange
      If TBVal = "" Then
         Ary = Evaluate("choosecols(" & .Address & ",14, 7, 5, 2, 3, 4)")
      Else
         Rws = Filter(Evaluate(Replace("transpose(if(isnumber(search(""" & TBVal & """,@)),row(@)-min(row(@))+1,""X""))", "@", .Columns(14).Address)), "X", False)
         If UBound(Rws) < 0 Then
            Me.ListBox1.List = Array("No matches")
            Exit Sub
         ElseIf UBound(Rws) = 0 Then
            ReDim Preserve Rws(1)
         End If
         Ary = Application.Index(.Value, Application.Transpose(Rws), Array(14, 5, 7, 2, 3, 4))
      End If
   End With
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub

Private Sub UserForm_Initialize()
   Dim Ary As Variant
   
   With Sheets("Sheet1").ListObjects("Table1").DataBodyRange
      Ary = Evaluate("choosecols(" & .Address & ",14, 7, 5, 2, 3, 4)")
   End With
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
End Sub
 
Upvote 0
It works perfectly. Thanks a million. I really appreciate your time.

One slight issue, the textbox may at times also contain somwhere in the data any of the following four characters [ ]"-. When that happens i get an error message, which ime assuming is as a result of the above being noncompatible with string variable. Can the above code be easily tweaked to solve the issue.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,400
Members
449,156
Latest member
LSchleppi

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