Listbox search at any position

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
I have a listbox that displays the list of files in a folder. i have a textbox that i want to use to search files, for instance, if i should type "ice" it should displays files like "rice, price, apprentice, iceland"
 
Ok, how about
Code:
Private Sub searchbox_Change()
   Dim Lst As Variant, NLst As Variant
   Dim i As Long, r As Long
   Static Srch As String
   
   If Len(Me.searchbox.Value) < Srch Then
      Me.FileList.Clear
      Call RecursiveFolder(FSO, StartFldr, Me.CheckBox1.Value)
   End If

   Lst = Me.FileList.List
   Srch = Me.searchbox.Value
   ReDim NLst(1 To UBound(Lst), 1 To 2)
   For i = 1 To UBound(Lst)
      If InStr(1, Lst(i, 1), Srch, 1) > 0 Then
         r = r + 1: NLst(r, 1) = Lst(i, 0): NLst(r, 2) = Lst(i, 1)
      End If
   Next i
   Me.FileList.List = NLst
End Sub
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Ok, how about
Code:
Private Sub searchbox_Change()
   Dim Lst As Variant, NLst As Variant
   Dim i As Long, r As Long
   Static Srch As String
   
   If Len(Me.searchbox.Value) < Srch Then
      Me.FileList.Clear
      Call RecursiveFolder(FSO, StartFldr, Me.CheckBox1.Value)
   End If

   Lst = Me.FileList.List
   Srch = Me.searchbox.Value
   ReDim NLst(1 To UBound(Lst), 1 To 2)
   For i = 1 To UBound(Lst)
      If InStr(1, Lst(i, 1), Srch, 1) > 0 Then
         r = r + 1: NLst(r, 1) = Lst(i, 0): NLst(r, 2) = Lst(i, 1)
      End If
   Next i
   Me.FileList.List = NLst
End Sub

it worked as expected.
it's just that the search is slow especially the folders that has plenty of files
 
Upvote 0
How about
Code:
Dim FSO As Object
Dim StartFldr As Object
Dim StartPth As String
Dim Lst As Variant

Private Sub CheckBox1_Click()
   Me.FileList.Clear
   Call RecursiveFolder(FSO, StartFldr, Me.CheckBox1.Value)
End Sub

Private Sub UserForm_Initialize()
   
   StartPth = "C:\mrexcel\"
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, False)
   Me.FileList.ColumnWidths = "0;200"
End Sub
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   Debug.Print Fldr.Name
   For Each FldrFile In Fldr.Files
      If FSO.GetExtensionName(FldrFile) Like "xls*" Then
         With Me.FileList
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
      End If
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
   Lst = Me.FileList.List
End Sub

Private Sub OK_Click()
    Workbooks.Open FileList.Value
End Sub


Private Sub searchbox_Change()
   Dim NLst As Variant
   Dim i As Long, r As Long
   Static Srch As String
   
   If Len(Me.searchbox.Value) < Srch Then
      Me.FileList.List = Lst
   End If

   Lst = Me.FileList.List
   Srch = Me.searchbox.Value
   ReDim NLst(1 To UBound(Lst), 1 To 2)
   For i = 1 To UBound(Lst)
      If InStr(1, Lst(i, 1), Srch, 1) > 0 Then
         r = r + 1: NLst(r, 1) = Lst(i, 0): NLst(r, 2) = Lst(i, 1)
      End If
   Next i
   Me.FileList.List = NLst
End Sub
 
Upvote 0
How about
Code:
Dim FSO As Object
Dim StartFldr As Object
Dim StartPth As String
Dim Lst As Variant

Private Sub CheckBox1_Click()
   Me.FileList.Clear
   Call RecursiveFolder(FSO, StartFldr, Me.CheckBox1.Value)
End Sub

Private Sub UserForm_Initialize()
   
   StartPth = "C:\mrexcel\"
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, False)
   Me.FileList.ColumnWidths = "0;200"
End Sub
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   Debug.Print Fldr.Name
   For Each FldrFile In Fldr.Files
      If FSO.GetExtensionName(FldrFile) Like "xls*" Then
         With Me.FileList
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
      End If
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
   Lst = Me.FileList.List
End Sub

Private Sub OK_Click()
    Workbooks.Open FileList.Value
End Sub


Private Sub searchbox_Change()
   Dim NLst As Variant
   Dim i As Long, r As Long
   Static Srch As String
   
   If Len(Me.searchbox.Value) < Srch Then
      Me.FileList.List = Lst
   End If

   Lst = Me.FileList.List
   Srch = Me.searchbox.Value
   ReDim NLst(1 To UBound(Lst), 1 To 2)
   For i = 1 To UBound(Lst)
      If InStr(1, Lst(i, 1), Srch, 1) > 0 Then
         r = r + 1: NLst(r, 1) = Lst(i, 0): NLst(r, 2) = Lst(i, 1)
      End If
   Next i
   Me.FileList.List = NLst
End Sub

this is more than great!
thanks
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Dear all, sorry to bother but I try to build the userform with the code of @Fluff

In the userform I have a checkbox, the function RecursiveFolder, a commandbutton Ok_Click, the code applied to the userform called UserForm_Initialize
but what is that code searchbox_change ? For a textbox, a listbox ? Let me know if you could give me any clarification :confused:
 
Upvote 0
Hi @yinkajewole, I tried unsuccessfully to reproduce the userform you used with the code of @Fluff. Could you post the file or a screenshot of the userform you used via dropbox ?

Many thanks and kind regards
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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