Identifying whether an excel file exists

  • Thread starter Thread starter JT
  • Start date Start date
J

JT

Hello group!

I have a (hopefully) quick query. Can anybody help?

I am writing a procedure and require a function which reads a file
path string and returns true or false depending on whether the file
exists on the user's computer. Does anybody out there know how this
can be achieved?

Thanks in advance.

John
 
Hi JT,

Before you use the below funtion, you have to get a reference to 'Microsoft
Scripting Runtime'

Function CheckFile() As Boolean
Dim mFile As FileSystemObject

Set mFile = New FileSystemObject
CheckFile = mFile.FileExists("C:\test\test.xls")
End Function

In the above function you can type your path or change the function to suit
your requirements.

HTH,
 
JT, try this

len(dir("C:\Test.txt")) > 0

Substitute your file for "C:\Test.txt"
 
Sub test()
If Dir("C:\Data\test.xls") <> "" Then
MsgBox "Exist"
Else
MsgBox "Not exist"
End If
End Sub

Just re-cast it as a Boolean function
 
Thanks to all of you, that was really helpful.

John Walkenbach wrote the Excel VBA for dummies book and I'm a fan of
his work.
 

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