check for valid file

  • Thread starter Thread starter pabs
  • Start date Start date
P

pabs

I'm using a routine to run thru a list of files and open them to modif
certain things.

here is the chunk of code that open uo the file and activates it

Workbooks.Open WindowName
Windows(WindowName + ".xls").Activate

how can check to see if a file exist before I attempt to open it?

the minute it hist the first line,if the file does not exist..i
crashes.
I need to be able to check first to be able to handle non existin
files.

thanks

Pab
 
Use DIR command

Several examples

===============
sPath$ = ThisWorkbook.Path & "\Data"
sFname$ = "Data.xls"
sFile$ = sPath & "\" & sFname

If Dir(sPath, vbDirectory) = "" Then
MkDir (sPath)
End If


===============
If Dir(sFile) = "" Then
Exit Sub
End If

===================

sDir = Dir(sPath & "\*.*")
Do
If sDir = "" Then
Exit Do
End If
more code here
sDir = Dir ' gets the next file
Loo
 
Back
Top