View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Karen Karen is offline
external usenet poster
 
Posts: 447
Default Code to open multiple files from within a folder

Thanks for your help guys.

Kind regards
Karen

"Karen" wrote:

Hi all,

I'm having problems with this script below. I need some Excel VBA code that
will open multiple files from within a folder. Sometimes there maybe 5 files
to open, sometimes there may be more. I will never know how many files will
be in the folder each time I execute the code, and I won't know the file
names in the folder either.

The script below is nearly correct, however it seems to retain the file
names from the last time it was executed. I need a way of flushing it out so
it doesn't retain anything when it ends. If it retains the file names then it
expects them to be there when it is next executed, which of corse causes
errors.

Thanks for any help with this - regards Karen. The script I'm using is
pasted below.

With Application.FileSearch
.NewSearch
.LookIn = "C:\testfolder"
.SearchSubFolders = False
.Filename = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
Workbooks.Open Filename:=.FoundFiles(i)
MsgBox .FoundFiles(i)
Next i
End If
End With