简体   繁体   English

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

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

I am attempting to rewrite some code that was using FileSearch for Excel 2003 VBA. 我试图重写一些使用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. 我试图调用一个应该确定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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM