Search Directories/SubDirectories UserForm

P

Patrick Kirk

Would anyone know how to create a userform that searches all subdirectories
for a particular name (ex: Any file with R0012 in it's name) and list the
findings within the form itself?

Another example, if I used the search function on Windows (start, search,
find...), the results should be within the userform with a scroll bar to show
all. Any help anyone can give will be greatly appreciated.
 
I

Incidental

Hi Patrick

The code below is pretty much the code stright from the excel VBA help
file. To test it create a userform with a listbox and a button and
paste the code below to userform module.

Option Explicit
Dim Fs As Object
Dim i As Integer

Private Sub CommandButton1_Click()

Set Fs = Application.FileSearch

With Fs
'Change below to give the name of the Directory you want to search
.LookIn = "C:\"
.SearchSubFolders = True
.Filename = "*R0012*"

If .Execute() > 0 Then

For i = 1 To .FoundFiles.Count

ListBox1.AddItem (.FoundFiles(i))

Next i

Else

MsgBox "There were no files found."

End If

End With

End Sub

Hope this helps

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top