If a file exists .... how do I code this

  • Thread starter Thread starter Alison
  • Start date Start date
A

Alison

I want to import a file to replace an existing one. I
code delete the first file - but if it doesnt exist my
macro crashes - how can I say If a file exists then do
the delete etc. and continue with the rest of the import

Grateful for any help

Alison
 
Add an error routine to catch the error and check for the
err code using a Select Case statement. Either do the
import from the error handler, or Resume Next to continue
your original code from the next line (ie. effectively
ignoring the delete method which caused the error)
 
Public Function FileExists(strPath As String, Optional lngType As Long) As
Boolean
On Error Resume Next
FileExists = Len(Dir(strPath, lngType)) > 0
End Function



If FileExists("some Path") = True Then
'do something
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top