如何使用VBA Excel 2007确定文件是否存在?

时间:2022-01-29 05:52:13

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.

我试图重写一些使用FileSearch for Excel 2003 VBA的代码。我正在尝试调用一个应该确定1或0的函数,并使用If语句执行一些代码或迭代到下一个文件。

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

1 个解决方案

#1


9  

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

#1


9  

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