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"
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
 
Upvote 0
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
 
Upvote 0
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 ?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
How are you currently populating the listbox?
 
Upvote 0
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"
 
Upvote 0
That code doesn't appear to populate the listbox.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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