Listbox search at any position

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
212
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
8,900
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
235
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
8,900
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
212
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
212
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,362
Office Version
365
Platform
Windows
How are you currently populating the listbox?
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
212
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,362
Office Version
365
Platform
Windows
That code doesn't appear to populate the listbox.
 

Forum statistics

Threads
1,082,382
Messages
5,365,128
Members
400,825
Latest member
Sreekanth_21

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top