2013-10-15 47 views
1

試圖編寫函數來檢測文件(pdf)是否存在。由於存在大量文件/文件夾,我想從單元格值構建文件路徑。Excel VBA - 基於單元格值檢測文件是否存在的函數

我有了這個迄今:

Public Function FileExists(FullpathName As String) As Boolean 

If Len(Dir(FullpathName)) = 0 Then 

    FileExists = True 

Else 

    FileExists = False 

End If 

End Function 

而且我在單元格中輸入此:

=FileExists(A2&B2&A3&" "&A1&" "&C2&".pdf") 

但它返回它爲假時,文件肯定是在那裏。任何人都可以點亮我失蹤的東西嗎?

謝謝!

+2

豈不是倒過來(TRUE;返回FALSE)? – Ioannis

+0

你有沒有在路徑的各個部分之間? – SeanC

+0

'A2&B2&A3&「」&A1&「」&C2&「。pdf」'調試這一行並告訴我們你得到了什麼? –

回答

3

IF條件落後,使用方法:

Public Function FileExists(FullpathName As String) As Boolean 
    If Len(Dir(FullpathName)) = 0 Then 
     FileExists = False 
    Else 
     FileExists = True 
    End If 
End Function 
+0

不是,那仍然不起作用,這可能與將單元包含在單元中有關嗎?比如A2目前是G:\ Documents \ –

+1

你需要做一些調試...........在你的UDF中包含這一行:** MsgBox FullPathName ** –

+0

是的,路徑名是正確的.. .still錯誤。離奇。 –

相關問題