2010-11-02 51 views
5

我正在嘗試重寫一些使用FileSearch for Excel 2003 VBA的代碼。我試圖調用一個應該確定1或0的函數,並使用If語句,我將執行一些代碼或迭代到下一個文件。如何確定文件是否存在使用VBA Excel 2007?

我沒有從我的函數返回正確的結果。

我的代碼:

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 

回答

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 
+0

非常感謝你。 – JohnM 2010-11-03 16:05:20

相關問題