Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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 使用工作簿。在已知文件位置打开会产生“0”;运行时错误1004<;文件>;“找不到”;_Excel_Vba - Fatal编程技术网

Excel 使用工作簿。在已知文件位置打开会产生“0”;运行时错误1004<;文件>;“找不到”;

Excel 使用工作簿。在已知文件位置打开会产生“0”;运行时错误1004<;文件>;“找不到”;,excel,vba,Excel,Vba,我正在构建一个实用程序,从特定文件中获取数据并将数据传输到另一个文件。源文件都具有相同的命名约定,其中包含一个相同的字符串,可用于将它们与目录中的其他文件区分开来。目的是将该字符串与通配符一起使用,以打开文件并提取所需的数据 我以为我有工作簿。昨天在查阅和之后打开了使用通配符,但今天我尝试运行我的测试代码,VBA声称找不到该文件(尽管文本中另有说明) 我的测试代码中没有太多错误: Dim strFilePath As String strFilePath = "C:\...\KMMacr

我正在构建一个实用程序,从特定文件中获取数据并将数据传输到另一个文件。源文件都具有相同的命名约定,其中包含一个相同的字符串,可用于将它们与目录中的其他文件区分开来。目的是将该字符串与通配符一起使用,以打开文件并提取所需的数据

我以为我有
工作簿。昨天在查阅和之后打开了
使用通配符,但今天我尝试运行我的测试代码,VBA声称找不到该文件(尽管文本中另有说明)

我的测试代码中没有太多错误:

Dim strFilePath As String
    strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"
Dim strFileName As String
    strFileName = "*SabaEntryForm.xlsx"

Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
错误为

找不到“TEST_KM6.7-3_BRSTA_SabaEntryForm.xlsx”。检查 文件名的拼写,并验证文件位置是否正确 对

和调试亮点

Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
因为错误明确提到了我希望打开的文件,并且因为我的代码没有给出具体的文件名,所以看起来VBA已经找到了该文件,即使错误的状态不是这样

VBA如何找到文件名,然后声称找不到?我做错了什么?

Dir()
只返回文件名,而不返回整个路径,因此如果当前目录与传递给Dir的文件夹不同,则会出现此问题

Dim strFilePath As String
    strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"

Dim strFileName As String
    strFileName = "*SabaEntryForm.xlsx"

Workbooks.Open FileName:=strFilePath & Dir$(strFilePath & strFileName), _
               ReadOnly:=True

谢谢,蒂姆。事实上,在
Dir$
调用前面添加
strFilePath&
解决了这个问题。我可能花了一些时间才发现
Dir$(strFilePath&strFileName)
只返回了文件名。