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

31,499
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
Share:
31,499

Related videos on Youtube

JohnM
Author by

JohnM

Updated on November 01, 2020

Comments

  • JohnM
    JohnM about 3 years

    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
    

Related