View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
GEdwards GEdwards is offline
external usenet poster
 
Posts: 40
Default Search Directories/SubDirectories and Display in ListBox or MsgBox

I have modified the code below from a reply to a question in 2007 from
Patrick Kirk. I am using MS Office Excel 2003.

For my use I would prefer the results within 2 possible scenarios;

1) a ListBox so that I may choose just see or select a file that was found
and proceed to work with it, such as open an XLS, if the file is an XLS file.
2) a MsgBox, but each file found must be displayed on separate lines within
the MsgBox

The difference for me too is that this IS NOT for a user form but rather a
macro that can be assigned to a button.

Suggestions?


Sub findFile()
Dim showThis As String
Dim i As Integer

Set Fs = Application.FileSearch
With Fs
'Change below to give the name of the Directory you want to search
.LookIn = "C:\Fun Stuff\Excel\"
.SearchSubFolders = True
.Filename = "msgbox"
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
showThis = showThis & " " & .FoundFiles(i)
'ListBox1.AddItem (.FoundFiles(i))
Next i
MsgBox .FoundFiles.Count & showThis
Else
MsgBox "No files found."
End If
End With
End Sub