Listbox search at any position

yinkajewole

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

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,735
Office Version
2007
Platform
Windows
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?
 

Fluff

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

Doflamingo

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

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,735
Office Version
2007
Platform
Windows
missing backslash
Code:
    wPath = "C:\trabajo\files\"
change the red text for your folder or what is the problem?
 

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
235
My mistake Dante :p, it works perfectly, I had forgotten the backlash at the end of the variable :)
 

yinkajewole

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

Forum statistics

Threads
1,081,620
Messages
5,360,072
Members
400,567
Latest member
SarahR2908

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