VBA错误1004尝试打开文件时找不到“文件名”

时间:2019-12-30 22:55:06

标签: excel vba

我正在尝试编写代码以打开指定文件夹中的所有Excel文件(.xlsx),并从每个文件夹中提取信息以添加到报表的主电子表格中。但是,我无法获取用于打开文件的代码。我收到运行时错误'1004',说找不到FILE.xlsx。但是,文件名是正确的并且匹配实际的文件名和扩展名(.xlsx)。代码的最后一行是行不通的,但是我不确定为什么。任何帮助将不胜感激,我只是在学习如何使用VBA。谢谢。

Dim FileName As String
Dim Folder As String
Dim Month As String
Dim File As Variant

Month = MonthName(CurrentMonth)
Folder = "C:\Users\mpresley\Documents\Other\Ben\Forecast Summary\Forecast"
FolderName = Folder & Month & CurrentYear

Dim firstEmptyRow As Long
Dim attachmentFolder As String, StrFile As String, filenameCriteria As String
Dim attachmentWorkBook As Workbook
Dim copyRngToArray As Variant

'# Set file extension
filenameCriteria = "xlsx"

'set
StrFile = Dir(FolderName & "\*" & filenameCriteria)
Do While Len(StrFile) > 0
    Set attachmentWorkBook = Workbooks.Open(StrFile)

1 个答案:

答案 0 :(得分:1)

同时指定文件夹路径和文件名:

resid 23 or resid 33 or resid 105 or resid 400