如何使用 VBA Excel 2007 确定文件是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4082539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I Determine If File Exists Using VBA Excel 2007?
提问by JohnM
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 Ifstatement 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
回答by Dr. belisarius
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

