Article ID: 155837
Article Last Modified on 10/11/2006
Sub FileSearchListBox()
' Dimension variables.
Dim myarray()
Dim fs As Object
Dim i As Integer
' Declare filesearch object.
Set fs = Application.FileSearch
' Set folder to search.
fs.LookIn = "c:\xldocs"
' Set file name to search for.
fs.FileName = "*.xls"
' Execute the file search, and check to see if the file(s) are
' present.
If fs.execute > 0 Then
' Redimension the array to the number of files found.
ReDim myarray(fs.FoundFiles.Count)
' Loop through all found file names and fill the array.
For i = 1 To fs.FoundFiles.Count
myarray(i) = fs.FoundFiles(i)
Next i
Else
' Display message if no files were found.
MsgBox "No files were found"
End If
' Loop through the array and fill the list box on the UserForm.
For i = 1 To fs.FoundFiles.Count
UserForm1.ListBox1.AddItem myarray(i)
Next i
' Display the UserForm.
UserForm1.Show
End Sub
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: file search directory list
Keywords: kbdtacode kbhowto kbprogramming KB155837