Excel VBA循环未在文件夹中的所有文件中循环
下面的代码在我选择的文件夹中的excel工作簿中循环,但由于某些原因,每次运行代码时都会跳过一个随机文件。例如,我选择的文件夹中有8个文件——它只在代码中的7个文件上执行复制粘贴任务。它也不是按顺序循环的——例如,它先循环第三个文件,然后循环第二个文件。。。等等Excel VBA循环未在文件夹中的所有文件中循环,excel,vba,Excel,Vba,下面的代码在我选择的文件夹中的excel工作簿中循环,但由于某些原因,每次运行代码时都会跳过一个随机文件。例如,我选择的文件夹中有8个文件——它只在代码中的7个文件上执行复制粘贴任务。它也不是按顺序循环的——例如,它先循环第三个文件,然后循环第二个文件。。。等等 'Retrieve Target Folder Path 'Opens folder picker and prompts user to navigate to current month's templates location
'Retrieve Target Folder Path 'Opens folder picker and prompts user to navigate to current month's templates location Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode 'If OK is selected
myPath = .SelectedItems(1) & "\" 'Assigns selected folder to myPath
End With
'If user does not select folder and hits Cancel NextCode: myPath = myPath If myPath = "" Then GoTo ResetSettings
'Target File Extension myExtension = "*.xls*"
'DIR function gets first file name in the folder 'with appropriate file
myFile = Dir(myPath & myExtension) 'Loop through each excel file in folder until DIR 'cannot find anymore
Do While myFile <> ""
'Opens the file and assigns to wb variable for future use
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Copy the balance worksheet(Sheet2)from each workbook
'in the specified folder and paste to master
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then
'Copy entire worksheet
wb.Worksheets(2).Cells.Copy
'PasteSpecial Values Only
wb.Worksheets(2).Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False 'Clears Clipboard
'Copy the values pasted in workbook
'and paste to the Master file
wb.Worksheets(2).Copy Before:=ThisWorkbook.Sheets(1)
'Set the name of the newly pasted values
'in the master file to the name of the workbook the values were copied from
ActiveSheet.Name = wb.Name
End If
Next Set wb = Nothing 'Close Workbook
Workbooks(myFile).Close SaveChanges:=False 'Ensure Workbook has closed before moving on to next line of code
DoEvents 'Get next file name
myFile = Dir Loop
'Message Box when tasks are completed MsgBox "Task Complete!"
End Sub
“检索目标文件夹路径”打开文件夹选择器并提示用户导航到当前月份的模板位置集FldrPicker=Application.FileDialog(msoFileDialogFolderPicker)
用FldrPicker
.AllowMultiSelect=False
如果.Show-1,则转到下一个代码“如果选择了“确定”
myPath=.SelectedItems(1)&“\”将所选文件夹分配给myPath
以
'如果用户未选择文件夹并点击取消下一步代码:myPath=myPath如果myPath=”“,则转到重置设置
'目标文件扩展名myExtension=“*.xls*”
“DIR函数获取文件夹中的第一个文件名”以及相应的文件名
myFile=Dir(myPath&myExtension)'循环遍历文件夹中的每个excel文件,直到Dir'无法再找到为止
当我的文件“”时执行此操作
'打开文件并分配给wb变量以供将来使用
设置wb=Workbooks.Open(文件名:=myPath&myFile)
'在继续下一行代码之前,确保工作簿已打开
多芬特
'从每个工作簿复制余额工作表(表2
'并粘贴到主文件夹中
对于工作簿中的每个wb
如果wb.Name ThisWorkbook.Name,则
'复制整个工作表
wb.工作表(2).单元格.副本
'仅粘贴特殊值
wb.工作表(2).范围(“A1”).粘贴特殊粘贴:=XLPasteValue
Application.CutCopyMode=False'清除剪贴板
'复制粘贴到工作簿中的值
'并粘贴到主文件
wb.工作表(2).之前复制:=此工作簿.工作表(1)
'设置新粘贴的值的名称
'将主文件中的值复制到工作簿的名称
ActiveSheet.Name=wb.Name
如果结束
下一组wb=Nothing'关闭工作簿
工作簿(myFile).Close SaveChanges:=False“请确保工作簿已关闭,然后再转到下一行代码。”
DoEvents获取下一个文件名
myFile=Dir循环
'任务完成时的消息框MsgBox“任务完成!”
端接头
它应该循环遍历文件夹中的所有文件,而不是。每次我运行代码时,它跳过的文件都是零星的。“ActiveSheet.Name=wb.Name”这一行真正应该做什么?我可能错了,但我认为您无法更改ActiveSheet.Name-您只能通过激活另一个工作表来完成此操作。请看以下内容:。我曾多次使用类似的代码循环浏览一个文件夹中的工作簿,甚至多个文件夹中的子文件夹,而且它始终有效-正如您所指出的,此代码按顺序循环。
myExtension
被注释掉,所以我怀疑myFile
是否是您所期望的。您使用了GoTo-NextCode
,但我看不到标签
,当用户单击取消
时实际会发生什么?Next Set wb=Nothing
和myFile=Dir循环
如何不抛出错误?“ActiveSheet.Name=wb.Name”这行实际上应该做什么?我可能错了,但我认为您无法更改ActiveSheet.Name-您只能通过激活另一个工作表来完成此操作。请看以下内容:。我曾多次使用类似的代码循环浏览一个文件夹中的工作簿,甚至多个文件夹中的子文件夹,而且它始终有效-正如您所指出的,此代码按顺序循环。myExtension
被注释掉,所以我怀疑myFile
是否是您所期望的。您使用了GoTo-NextCode
,但我看不到标签
,当用户单击取消
时实际会发生什么?Next Set wb=Nothing
和myFile=Dir循环如何不抛出错误?