View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Check Folder For Files

Dim myNames as variant
dim wkbk as workbook
dim myPath as string
dim iCtr as long

mynames = array("filename1.xls", _
"filename2.xls", _
"filename24.xls") 'you'd type in all 24 of those names


mypath = "C:\my documents\excel" 'or whatever you used to get the path
if right(mypath,1) < "\" then
mypath = mypath & "\"
end if

for ictr = lbound(mynames) to ubound(mynames)
set wkbk = workbooks.open(filename:=mypath & mynames(ictr)
'do stuff with wkbk
wkbk.close savechanges:=false 'or true??
next ictr



Dave wrote:

Hi Mike H,

I don't really know how to produce a loop. I have a total of 24 files I want
to find in a folder, all with set names. Could you explain how I could loop
through these?

Also, if a file exists, I don't want anything to show - only when a file
doesn't exist do I need a popup.

Thanks!
Dave

"Mike H" wrote:

Dave,

Paste this code in and it does it for the named fole File1.xls. To loop
through several files and check for their existence you could put the names
to search for an a worksheets and loop through that. If you stuck in doing
that post back.


Sub MACRO()
MsgBox ("Select the Folder")
Set foldername1 = Application.FileDialog(msoFileDialogFolderPicker)
foldername1.Show
foldername1path = CurDir()
If Dir(foldername1path & "book1.xls") < "" Then
MsgBox "File exists"
Else
MsgBox "File does not exist"
End If
End Sub

Mike

"Dave" wrote:

Hi Guys,

I'd like to do the following in a macro:

Sub MACRO()

MsgBox ("Select the Folder")
Set foldername1 = Application.FileDialog(msoFileDialogFolderPicker)
foldername1.Show
foldername1path = CurDir()

'HELP HERE

End Sub

I want to then check the folder for files:

file1.xls
file245.xls
file278.xls
file88.xls

etc.

so they'll be something like foldername1path & "\" file1.xls (AM I RIGHT?)

If one or more of these files do not exist, I would like a MsbBox to show
with something like "File1.xls and File88.xls not found".

Thanks in advance
Dave


--

Dave Peterson