JonasTiger
New Member
- Joined
- Jan 28, 2022
- Messages
- 28
- Office Version
- 365
- 2019
- Platform
- Windows
Hi
I have a userform with a listbox and I want to create a searchbox to filter the listbox while typing.
I was searching for a solution for a similar issue and I found this that thread:
I have data in a table named "BD_CLIENTES", in a sheet with the same name.
Table BD_CLIENTES has a range "B:M" (12 columns)
I can´t populate the ListBox, the only result I get is data from B col, which is ID_CLIENTES (numeric format),as the picture shows:
Thank you very much in advance
I have a userform with a listbox and I want to create a searchbox to filter the listbox while typing.
I was searching for a solution for a similar issue and I found this that thread:
But I'm having trouble to adapt the code to my case and I would like help to see what I'm doing wrong:Filter a listbox containing multiple fields/columns using a single searchbox
Here is a link explaining exactly what I am trying to accomplish using excel. I am trying to use only ONE textbox that will dynamically search (search any character as I type) through multiple columns and have the listbox filter base off of text in...
www.mrexcel.com
I have data in a table named "BD_CLIENTES", in a sheet with the same name.
Table BD_CLIENTES has a range "B:M" (12 columns)
I can´t populate the ListBox, the only result I get is data from B col, which is ID_CLIENTES (numeric format),as the picture shows:
VBA Code:
Private Sub UserForm_Initialize()
Dim sArr(), SRan As Range
Me.OptionButton1 = True
Set SRan = Range(Range("B2"), Range("B2").End(xlDown).End(xlToRight))
ReDim sArr(1 To SRan.Rows.Count, 1 To SRan.Columns.Count)
sArr = SRan.Value
sArr = bbSort(sArr)
Me.ListBox1.List = sArr
End Sub
Function bbSort(ByVal lArr) As Variant
Dim tTmp
On Error Resume Next
UB2 = UBound(lArr, 2)
On Error GoTo 0
If ISort < 50 And UB2 > 1 Then
lb0 = LBound(lArr)
For i = lb0 To UBound(lArr) - 1
For j = i + 1 To UBound(lArr)
If UCase(lArr(i, lb0 + ISort)) > UCase(lArr(j, lb0 + ISort)) Then
For k = LBound(lArr, 2) To UBound(lArr, 2)
tTmp = lArr(j, k)
lArr(j, k) = lArr(i, k)
lArr(i, k) = tTmp
Next k
End If
Next j
Next i
End If
bbSort = lArr
End Function
Private Sub TextBox1_Change()
'ListBox content is updated whenever the (filter) textbox is modified
Dim SRan As Range, ohYes As Boolean, rCount As Long
'
Set SRan = Range(Range("B2"), Range("B2").End(xlDown).End(xlToRight)) 'This is the Row Source
ReDim sArr(1 To SRan.Columns.Count, 1 To SRan.Rows.Count)
For i = 1 To SRan.Rows.Count
ohYes = False
For j = 1 To SRan.Columns.Count
If InStr(1, SRan.Cells(i, j).Value, TextBox1.Value, vbTextCompare) > 0 Then
ohYes = True
Exit For
End If
Next j
If ohYes Then
rCount = rCount + 1
For j = 1 To SRan.Columns.Count
sArr(j, rCount) = SRan.Cells(i, j).Value
Next j
End If
Next i
'Resize sArr:
If rCount > 0 Then
ReDim Preserve sArr(1 To j - 1, 1 To rCount)
Else
ReDim Preserve sArr(1 To j - 1, 1 To 1)
End If
'Sort the array:
If UBound(sArr, 2) > 1 Then
'sArr is a true array:
sArr = bbSort(Application.WorksheetFunction.Transpose(sArr)) 'REMOVE this line if you don't need Sort
ListBox1.List = sArr
Else
'If one line only it is a bit more complex:
Me.ListBox1.Clear
Me.ListBox1.AddItem
For i = 1 To UBound(sArr)
Me.ListBox1.Column(i - 1, 0) = sArr(i, 1)
Next i
End If
End Sub
Thank you very much in advance