在循环中添加打开和关闭命令时,Excel VBA Dir循环失败
我正在尝试从具有相同前缀的目录中的所有Excel文件导入数据。数据将从这些文件聚合并写入主文件。我已经成功地构建了一个do-while脚本,它可以使用带有通配符的DIR来识别三个测试文件。但是,当我添加打开文件(并立即关闭)的命令时,do while在打开和关闭文件后第一次通过后失败。注释掉打开和关闭命令以及do while循环,循环三次以识别测试文件。最后,我想用对sub的调用来替换open/close命令,sub将打开文件、聚合数据并将其写入主文件。我提到这一点是为了防止它改变编码方式。我搜索了论坛,找到了一些其他方法来实现我的一些目标,但不是全部。一个例子是文件名中的通配符。感谢您的帮助在循环中添加打开和关闭命令时,Excel VBA Dir循环失败,excel,vba,Excel,Vba,我正在尝试从具有相同前缀的目录中的所有Excel文件导入数据。数据将从这些文件聚合并写入主文件。我已经成功地构建了一个do-while脚本,它可以使用带有通配符的DIR来识别三个测试文件。但是,当我添加打开文件(并立即关闭)的命令时,do while在打开和关闭文件后第一次通过后失败。注释掉打开和关闭命令以及do while循环,循环三次以识别测试文件。最后,我想用对sub的调用来替换open/close命令,sub将打开文件、聚合数据并将其写入主文件。我提到这一点是为了防止它改变编码方式。我搜
Sub LoopThroughFiles()
Dim strName As String
Dim strPath As String
Dim strFile As String
strPath = ThisWorkbook.Path
strName = "Employee Gross Sales"
strFile = Dir(strPath & "\" & strName & "*")
Do While Len(strFile) > 0
Debug.Print strFile
' Call OpenFile(strPath, strFile) <-- Eventually will replace open / close commands below
Workbooks.Open FileName:=strPath & "\" & Dir(strPath & "\" & strFile)
' Read / Aggregate / Write data code here or in called sub
Workbooks(strFile).Close SaveChanges:=False
strFile = Dir
Loop
End Sub
Sub OpenFile(strPath, strFile)
Dim wbTarget, wbSource As Workbook
Set wbSource = Workbooks.Open(FileName:=strPath & "\" & Dir(strPath & "\" & strFile))
wbSource.Close SaveChanges:=False
End Sub
Sub-LoopThroughFiles()
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
strPath=ThisWorkbook.Path
strName=“员工总销售额”
strFile=Dir(strPath&“\”&strName&“*”)
当Len(strFile)>0时执行
调试。打印strFile
'在工作簿中调用OpenFile(strPath,strFile)您的Dir(strPath&“\”&strFile)
。Open
命令正在“覆盖”您的原始Dir
-此时您应该只使用strFile
如果您将当前代码缩减到仅受目录影响的位,它将如下所示:
strFile = Dir(some_string_including_wildcard)
'The above statement returns the first file name matching the wildcarded expression
Do While Len(strFile) > 0
... Dir(specific_filename_being_processed) ...
'The above statement finds the first file name matching the specific filename
'which will obviously be the specific filename
strFile = Dir
'That statement gets the next file name matching the argument last used as
' a parameter to a Dir. As the last argument was a specific file, and there
' are no more files matching that argument (because it contained no wildcards)
' this returns an empty string.
Loop
您的代码应编写为:
Sub LoopThroughFiles()
Dim strName As String
Dim strPath As String
Dim strFile As String
strPath = ThisWorkbook.Path
strName = "Employee Gross Sales"
strFile = Dir(strPath & "\" & strName & "*")
Do While Len(strFile) > 0
Debug.Print strFile
' OpenFile strPath, strFile ' <-- Eventually will replace open / close commands below
Workbooks.Open FileName:=strPath & "\" & strFile
' Read / Aggregate / Write data code here or in called sub
Workbooks(strFile).Close SaveChanges:=False
strFile = Dir
Loop
End Sub
Sub OpenFile(strPath As String, strFile As String)
Dim wbTarget As Workbook, wbSource As Workbook
Set wbSource = Workbooks.Open(FileName:=strPath & "\" & strFile)
wbSource.Close SaveChanges:=False
End Sub
Sub-LoopThroughFiles()
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
strPath=ThisWorkbook.Path
strName=“员工总销售额”
strFile=Dir(strPath&“\”&strName&“*”)
当Len(strFile)>0时执行
调试。打印strFile
“OpenFile strPath,strFile”您的目录(strPath&“\”&strFile)
位于工作簿中。Open
命令正在“覆盖”您的原始目录
-此时您应该只使用strFile
如果您将当前代码缩减到仅受目录影响的位,它将如下所示:
strFile = Dir(some_string_including_wildcard)
'The above statement returns the first file name matching the wildcarded expression
Do While Len(strFile) > 0
... Dir(specific_filename_being_processed) ...
'The above statement finds the first file name matching the specific filename
'which will obviously be the specific filename
strFile = Dir
'That statement gets the next file name matching the argument last used as
' a parameter to a Dir. As the last argument was a specific file, and there
' are no more files matching that argument (because it contained no wildcards)
' this returns an empty string.
Loop
您的代码应编写为:
Sub LoopThroughFiles()
Dim strName As String
Dim strPath As String
Dim strFile As String
strPath = ThisWorkbook.Path
strName = "Employee Gross Sales"
strFile = Dir(strPath & "\" & strName & "*")
Do While Len(strFile) > 0
Debug.Print strFile
' OpenFile strPath, strFile ' <-- Eventually will replace open / close commands below
Workbooks.Open FileName:=strPath & "\" & strFile
' Read / Aggregate / Write data code here or in called sub
Workbooks(strFile).Close SaveChanges:=False
strFile = Dir
Loop
End Sub
Sub OpenFile(strPath As String, strFile As String)
Dim wbTarget As Workbook, wbSource As Workbook
Set wbSource = Workbooks.Open(FileName:=strPath & "\" & strFile)
wbSource.Close SaveChanges:=False
End Sub
Sub-LoopThroughFiles()
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
strPath=ThisWorkbook.Path
strName=“员工总销售额”
strFile=Dir(strPath&“\”&strName&“*”)
当Len(strFile)>0时执行
调试。打印strFile
“OpenFile strPath,strFile”您是否尝试过单步执行代码以检查出错时的值?我确实尝试过单步执行代码。当我注释掉Open和Close命令时,strFile=Dir就在循环将填充下一个文件名之前。对行和strFile=Dir进行去注释会变成“”,但我不知道为什么。YowE3K帮了我的忙。你有没有试过在代码出错时单步检查值?我确实试过单步检查代码。当我注释掉Open和Close命令时,strFile=Dir就在循环将填充下一个文件名之前。对行和strFile=Dir进行去注释会变成“”,但我不知道为什么。YowE3K帮我解决了问题。谢谢YowE3K解释并让代码正常工作。干杯。谢谢你,YowE3K,它解释了代码并使代码正常工作。干杯