Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Determine If File Exists Using VBA Excel 2007?

Tags:

excel

vba

I am attempting to rewrite some code that was using FileSearch for Excel 2003 VBA. I'm attempting to call a function that should determine 1 or 0 and using an If statement I will execute some code or iterate to the next file.

I am not returning the correct result from my function.

My code:

 Dim MyDir As String, Fn As String
 Dim MyFile As String

   MyDir = "C:Test\"
   Fn = "" & "" & Examiner & " " & MnName & " " & Yr & ".xls"
   MyFile = MyDir & """" & Fn & """"

    If FileThere(MyFile) Then
    MsgBox yes

    Else
    MsgBox Not there

    End If

    '''''''''''''''''
    Function FileThere(FileName As String) As Boolean
         FileThere = (Dir(FileName) > "")
    End Function
like image 841
JohnM Avatar asked Nov 02 '10 22:11

JohnM


1 Answers

Sub a()

MsgBox "test1 " & FileThere("c:\test1.bat")
MsgBox "k1" & FileThere("c:\k1")

End Sub

Function FileThere(FileName As String) As Boolean
     If (Dir(FileName) = "") Then
        FileThere = False
     Else:
        FileThere = True
     End If
End Function
like image 67
Dr. belisarius Avatar answered Oct 01 '22 08:10

Dr. belisarius