Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 检查vb中是否存在文件_Excel_Vba - Fatal编程技术网

Excel 检查vb中是否存在文件

Excel 检查vb中是否存在文件,excel,vba,Excel,Vba,我试图运行宏来检查文件是否存在。我得到编译错误,未定义子函数或函数。有人能帮忙吗 If FileExists(filepath) Then Else Call Master Call PrinttoPDF End If 试试下面的潜艇 Sub CheckFilePath() If Dir(FilePath, vbNormal) <> "" Then Call Master Call PrinttoPDF Else

我试图运行宏来检查文件是否存在。我得到编译错误,未定义子函数或函数。有人能帮忙吗

If FileExists(filepath) Then

Else
  Call Master
  Call PrinttoPDF
End If
试试下面的潜艇

Sub CheckFilePath()
    If Dir(FilePath, vbNormal) <> "" Then
        Call Master
        Call PrinttoPDF
    Else
        MsgBox "File does not exists."
    End If
End Sub
子检查文件路径()
如果Dir(FilePath,vbNormal)“,则
呼叫主机
调用PrinttoPDF
其他的
MsgBox“文件不存在。”
如果结束
端接头

我不是VBA大师,但它看起来要么是
文件存在
主文件
,要么是
PrinttoPDF
不作为子文件或函数存在。可能改变情况,最后一个可能是
PrintToPdf

我希望错误会告诉您错误发生在哪一行

我发现了一个工作示例,您可以通过该示例:

Sub Test_File_Exist_With_Dir()
    Application.ScreenUpdating = False
    Dim FilePath As String
    FilePath = ""
    On Error Resume Next
    FilePath = Dir("C:\Users\DT168\Desktop\Test folder\Book2.xlsx")
    On Error GoTo 0
    If FilePath = "" Then
        MsgBox "File doesn't exist", vbInformation, "Kutools for Excel"
    Else
        MsgBox "File exist", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = False
End Sub

你好实际上,变量FilePath是文件名及其完整路径。建议的更改正在运行else部分,即使文件存在于路径位置中。您有什么进一步的建议吗?我已经测试了代码,运行良好。请仔细检查您的文件路径。你能把完整的代码放在这里看看吗。