Check a file exists?

  • Thread starter Thread starter andycharger
  • Start date Start date
A

andycharger

In my macro, I am opening several other spreadsheets and performing som
formatting.
However, sometimes a particular spreadsheet may not be present.

What I need to find out is if there is any way to check if a fil
exists before performing the code i need.

Like this:

If (spreadsheet exists) then
' do the code

Else

End if

So what I need to know is if there is a keyword or something to do th
check. If so, what is it?

Please help!

And
 
if you mean before you attempt to open it

if dir("C:\MyFolder\Myfile.xls") = "" then
' file doesn't exist
Else
workbooks.Open "C:\MyFolder\Myfile.xls"
' process file
End if

If you mean is it already open in excel

Dim wkbk as workbook
On Error Resume Next
set wkbk = workbooks("MyFile.xls")
On Error goto 0
if wkbk is nothing then
msgbox "Myfile.xls is not open in Excel"
End if
 
Back
Top