Listbox search at any position

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
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
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
210
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
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
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
210
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
235
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:
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
31,991
Office Version
365
Platform
Windows
It's a textbox
 

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
235
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
 

Forum statistics

Threads
1,081,543
Messages
5,359,431
Members
400,526
Latest member
Brook1083

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top