Textbox to search Listbox

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
I would like to have a code that will search the items in my listbox. The search should not be case sensitive. This is my code
Code:
[COLOR=#333333]Private Sub UserForm_Initialize()[/COLOR]Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
MyFolder = "C:\Test"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
    ListBox1.AddItem MyFile
    MyFile = Dir
Loop [COLOR=#333333]End Sub[/COLOR]
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this code.
Code:
Option Explicit
Dim arrAllFiles As Variant

Private Sub TextBox1_Change()
Dim arrFilter As Variant

    If TextBox1.Value = "" Then
        ListBox1.List = arrAllFiles
    End If
    arrFilter = Filter(arrAllFiles, TextBox1.Value, , vbTextCompare)
    
    If UBound(arrFilter) <> -1 Then
        ListBox1.List = arrFilter
    Else
        ListBox1.Clear
    End If
End Sub

Private Sub UserForm_Initialize()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer

    MyFolder = "C:\Test"
    MyFile = Dir(MyFolder & "\*.xls")
    
    Do While MyFile <> ""
        ListBox1.AddItem MyFile
        MyFile = Dir
    Loop
    
    arrAllFiles = ListBox1.List
    
    ReDim Preserve arrAllFiles(0 To ListBox1.ListCount - 1, 0)
    
    arrAllFiles = Application.Transpose(arrAllFiles)
    
End Sub
 
Upvote 0
Try this code.
Code:
Option Explicit
Dim arrAllFiles As Variant

Private Sub TextBox1_Change()
Dim arrFilter As Variant

    If TextBox1.Value = "" Then
        ListBox1.List = arrAllFiles
    End If
    arrFilter = Filter(arrAllFiles, TextBox1.Value, , vbTextCompare)
    
    If UBound(arrFilter) <> -1 Then
        ListBox1.List = arrFilter
    Else
        ListBox1.Clear
    End If
End Sub

Private Sub UserForm_Initialize()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer

    MyFolder = "C:\Test"
    MyFile = Dir(MyFolder & "\*.xls")
    
    Do While MyFile <> ""
        ListBox1.AddItem MyFile
        MyFile = Dir
    Loop
    
    arrAllFiles = ListBox1.List
    
    ReDim Preserve arrAllFiles(0 To ListBox1.ListCount - 1, 0)
    
    arrAllFiles = Application.Transpose(arrAllFiles)
    
End Sub

I don't like this method, I'll appreciate another code
 
Upvote 0
What's wrong with the method?
this line
Code:
[COLOR=#333333][I]arrAllFiles = Application.Transpose(arrAllFiles)[/I][/COLOR]
gives me error. Moreover, typing the first letter do not start searching until I type the second letter
 
Upvote 0
What error do you get?

Is the listbox populated?
 
Upvote 0
I've replace Application.Transpose with a function that should work in VBA outwith Excel.
Code:
Option Explicit

Dim arrAllFiles As Variant

Private Sub TextBox1_Change()
Dim arrFilter As Variant

    If TextBox1.Value = "" Then
        ListBox1.List = arrAllFiles
    End If
    arrFilter = Filter(arrAllFiles, TextBox1.Value, , vbTextCompare)
    
    If UBound(arrFilter) <> -1 Then
        ListBox1.List = arrFilter
    Else
        ListBox1.Clear
    End If
End Sub

Private Sub UserForm_Initialize()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer

    MyFolder = "C:\Test"
    MyFile = Dir(MyFolder & "\*.xls")
    
    Do While MyFile <> ""
        ListBox1.AddItem MyFile
        MyFile = Dir
    Loop
    
    arrAllFiles = ListBox1.List
    
    ReDim Preserve arrAllFiles(0 To ListBox1.ListCount - 1, 0)
    
    arrAllFiles = FlattenArray(arrAllFiles)
    
End Sub

Function FlattenArray(arr As Variant) As Variant
Dim arrOut As Variant
Dim I As Long

    ReDim arrOut(LBound(arr, 1) To UBound(arr, 1))
    
    For I = LBound(arrOut, 1) To UBound(arrOut, 1)
        arrOut(I) = arr(I, UBound(arr, 2))
    Next I
    
    FlattenArray = arrOut
    
End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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