Listbox search at any position

yinkajewole

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

sadboy309

Board Regular
Joined
Oct 15, 2014
Messages
88
From with a Combobox1, a Listbox1:

Code:
'Code in Form

Private fl As Boolean
Private nFld As String


Private Sub ComboBox1_Change()
If Not fl Then
    fl = True
    If Not InStr(1, nFld, ComboBox1) > 0 Then
        ComboBox1 = nFld & ComboBox1 & "\"
    End If
    nFld = ComboBox1
    Call GetSubFolderNames(ComboBox1, ComboBox1)
    Call GetFileNames(ListBox1, ComboBox1)
    fl = False
End If
End Sub


Private Sub UserForm_Initialize()
fl = True
nFld = "C:\"
ComboBox1 = nFld
Call GetSubFolderNames(ComboBox1, ComboBox1)
Call GetFileNames(ListBox1, ComboBox1)
fl = False
End Sub


'######################### Get Data Explorer ######################


Private Sub GetSubFolderNames(cmBx As Variant, NameFolder As String)
On Error GoTo iExit
Dim MyFSO As Object
Dim MyFile As Object
Dim MyFolder As Object
Dim MySubFolder As Object


Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(NameFolder)


cmBx.Clear
For Each MySubFolder In MyFolder.SubFolders
    cmBx.AddItem MySubFolder.Name
Next


iExit:
End Sub


Sub GetFileNames(lstBx As Variant, NameFolder As String)
On Error GoTo iExit
Dim MyFSO As Object
Dim MyFile As Object
Dim MyFolder As Object


Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(NameFolder)


lstBx.Clear
For Each MyFile In MyFolder.Files
    If InStr(1, MyFile.Name, "ice") > 0 Then
        lstBx.AddItem MyFile.Name
    End If
Next MyFile


iExit:
End Sub
source link code: https://trumpexcel.com/vba-filesystemobject/#Example-4-Get-the-List-of-All-Sub-folders-in-a-Folder
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,835
Office Version
2007
Platform
Windows
Try this:

Change "C:\trabajo\files" For the name of your folder

Code:
Dim wPath '<<---Up to all the code


Private Sub TextBox1_Change()
    ListBox1.Clear
    arch = Dir(wPath & "*.*")
    Do While arch <> ""
        If arch Like "*" & TextBox1.Value & "*" Then
            ListBox1.AddItem arch
        End If
        arch = Dir()
    Loop
End Sub


Private Sub UserForm_Activate()
    wPath = [COLOR=#ff0000]"C:\trabajo\files\"[/COLOR]
    arch = Dir(wPath & "*.*")
    Do While arch <> ""
        ListBox1.AddItem arch
        arch = Dir()
    Loop
End Sub
Let me know if you have any doubt
 

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
232
Hi Dante, I've tried your code with a userform having a listbox and a textbox. I've changed the variable wpath but when I write in the textbox the first letters of the file I would like to see in the listbox, it gives me nothing in the listbox while when there is nothing written in the textbox, 3 files appear in the listbox. Weird because those file don't exist in my computer ...:confused:

Any ideas ?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,835
Office Version
2007
Platform
Windows
Hi Dante, I've tried your code with a userform having a listbox and a textbox. I've changed the variable wpath but when I write in the textbox the first letters of the file I would like to see in the listbox, it gives me nothing in the listbox while when there is nothing written in the textbox, 3 files appear in the listbox. Weird because those file don't exist in my computer ...:confused:

Any ideas ?
Hi @Doflamingo

You can put the code you use
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
Try this:

Change "C:\trabajo\files" For the name of your folder

Code:
Dim wPath '<<---Up to all the code


Private Sub TextBox1_Change()
    ListBox1.Clear
    arch = Dir(wPath & "*.*")
    Do While arch <> ""
        If arch Like "*" & TextBox1.Value & "*" Then
            ListBox1.AddItem arch
        End If
        arch = Dir()
    Loop
End Sub


Private Sub UserForm_Activate()
    wPath = [COLOR=#ff0000]"C:\trabajo\files\"[/COLOR]
    arch = Dir(wPath & "*.*")
    Do While arch <> ""
        ListBox1.AddItem arch
        arch = Dir()
    Loop
End Sub
Let me know if you have any doubt
It did not work at all
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
From with a Combobox1, a Listbox1:

Code:
'Code in Form

Private fl As Boolean
Private nFld As String


Private Sub ComboBox1_Change()
If Not fl Then
    fl = True
    If Not InStr(1, nFld, ComboBox1) > 0 Then
        ComboBox1 = nFld & ComboBox1 & "\"
    End If
    nFld = ComboBox1
    Call GetSubFolderNames(ComboBox1, ComboBox1)
    Call GetFileNames(ListBox1, ComboBox1)
    fl = False
End If
End Sub


Private Sub UserForm_Initialize()
fl = True
nFld = "C:\"
ComboBox1 = nFld
Call GetSubFolderNames(ComboBox1, ComboBox1)
Call GetFileNames(ListBox1, ComboBox1)
fl = False
End Sub


'######################### Get Data Explorer ######################


Private Sub GetSubFolderNames(cmBx As Variant, NameFolder As String)
On Error GoTo iExit
Dim MyFSO As Object
Dim MyFile As Object
Dim MyFolder As Object
Dim MySubFolder As Object


Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(NameFolder)


cmBx.Clear
For Each MySubFolder In MyFolder.SubFolders
    cmBx.AddItem MySubFolder.Name
Next


iExit:
End Sub


Sub GetFileNames(lstBx As Variant, NameFolder As String)
On Error GoTo iExit
Dim MyFSO As Object
Dim MyFile As Object
Dim MyFolder As Object


Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(NameFolder)


lstBx.Clear
For Each MyFile In MyFolder.Files
    If InStr(1, MyFile.Name, "ice") > 0 Then
        lstBx.AddItem MyFile.Name
    End If
Next MyFile


iExit:
End Sub
source link code: https://trumpexcel.com/vba-filesystemobject/#Example-4-Get-the-List-of-All-Sub-folders-in-a-Folder
This code does not address my question
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,275
How are you currently populating the listbox?
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
How are you currently populating the listbox?
Code:
   StartPth = folder_TextBox.Text
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, False)
   Me.Filelist.ColumnWidths = "0;200"
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,275
That code doesn't appear to populate the listbox.
 

Forum statistics

Threads
1,078,137
Messages
5,338,458
Members
399,234
Latest member
WaddoAU

Some videos you may like

This Week's Hot Topics

Top