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"
 
That code doesn't appear to populate the listbox.
this is the rest of the code
Code:
Dim FSO As Object
Dim StartFldr As Object
Dim StartPth As String

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

Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   
   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
End Sub

Private Sub folder_TextBox_change()
  

   StartPth = folder_TextBox.Text
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Me.Filelist.Clear
   Call RecursiveFolder(FSO, StartFldr, False)
   Me.Filelist.ColumnWidths = "0;200"

  

End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Really, it works but the files were invisible. Actually, my listbox that contains the files is ColumnCount -1. Perhaps that's the cause...

any modified code to suit that?

I do not understand, if the code works, then what do you want to adapt?
 
Upvote 0
How about
Code:
Private Sub searchbox_Change()
   Dim Lst As Variant, NLst As Variant
   Dim i As Long, r As Long
   Dim Srch As String
   
   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
Hello Dante,

I've applied this code to the textbox

Private Sub UserForm_Activate()
wPath = "C:\trabajo\files"
arch = Dir(wPath & "*.*")
Do While arch <> ""
ListBox1.AddItem arch
arch = Dir()
Loop
End Sub
 
Upvote 0
missing backslash
Code:
    wPath = "C:\trabajo\files\"
change the red text for your folder or what is the problem?
 
Upvote 0
How about
Code:
Private Sub searchbox_Change()
   Dim Lst As Variant, NLst As Variant
   Dim i As Long, r As Long
   Dim Srch As String
   
   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

yes, this worked for me. But the problem is that if i should press a backspace to delete a letter from the search box, the files remain invisible. Also, by the time i delete all the letters i had written, it will not show the files again until i restart the form
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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