[英]Copy merge specific worksheets to one workbook
I am trying to copy a worksheet called "application" from all identical files in a folder, into a master workbook and rename the copied worksheet in the name of the file its been copied from. 我正在尝试将一个文件夹中所有相同文件中的一个名为“应用程序”的工作表复制到一个主工作簿中,并使用复制源文件的名称重命名复制的工作表。 So far my code copies everything and I cannot get it to rename the copied worksheet to name of file it came from.
到目前为止,我的代码已复制了所有内容,但我无法获得将其复制后的工作表重命名为其来源文件的名称的信息。
Thank you 谢谢
Sub GetSheets()
Application.ScreenUpdating = False
Path = "C:\Users\Desktop\Work docs\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
If Sheet.Name = "application" Then
End If
Sheets.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close SaveChanges:=False
Filename = Dir()
Loop
Application.ScreenUpdating = True
End Sub
Your IF condition is closing before you are copying 'application' sheet, so Sheets.Copy
will just copy all the sheets from your workbook. 在复制“应用程序”工作表之前,您的IF条件即将关闭,因此
Sheets.Copy
只会复制工作簿中的所有工作表。 You can try the below code: 您可以尝试以下代码:
Do While Filename <> ""
Workbooks.Open Filename:=Path1 & Filename, ReadOnly:=True
For Each Sheet In Workbooks(Filename).Sheets
If Sheet.Name = "application" Then
Sheet.Copy After:=ThisWorkbook.Sheets(1)
ThisWorkbook.Sheets("application").Name = Filename & "-application" 'Changes the sheetname from "application" of test1.xls workbook to "test1.xls-application"
End If
Next Sheet
Workbooks(Filename).Close SaveChanges:=False
Filename = Dir()
Loop
I was not able to use Path as a variable (maybe due to some system configuration - need to check why), so I have used Path1 instead. 我无法将Path用作变量(可能由于某些系统配置-需要检查原因),所以我改用Path1 。 You can use
ActiveWorkbook.Sheets
also instead of Workbooks(Filename).Sheets
. 您还可以使用
ActiveWorkbook.Sheets
代替Workbooks(Filename).Sheets
。 However I feel its better to reference a workbook by its name. 但是,我觉得最好以其名称引用工作簿。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.