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.

 
The only one that should cause a problem is the ".
Try
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
         TBVal = Replace(TBVal, Chr(34), Chr(34) & Chr(34))
         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
 
Upvote 0
Solution

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The only one that should cause a problem is the ".
Try
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
         TBVal = Replace(TBVal, Chr(34), Chr(34) & Chr(34))
         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
It works Perfectly!

Thanks a mil. You've helped save me hours of work.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,356
Messages
6,124,475
Members
449,164
Latest member
Monchichi

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