[英]Copying worksheets from multiple workbooks into existing worksheets in a different workbook
I have a template workbook with tab names of "Extract 1, Extract 2, Extract 3" etc and a main Summary page that contains formulas that rely on all of these tabs. 我有一个模板工作簿,其标签名称为“ Extract 1,Extract 2,Extract 3”等,其主摘要页面包含依赖于所有这些标签的公式。 I also have a number of workbooks (22) that each contain one worksheet with a data extract in them. 我也有许多工作簿(22),每个工作簿都包含一个工作表,其中包含数据提取。 I need to be able to loop through these workbooks and copy the sheets over without the need to remove and insert a new tab (needs to use existing tabs). 我需要能够遍历这些工作簿并复制工作表,而无需删除并插入新的选项卡(需要使用现有的选项卡)。 Initially I had this: 最初我有这个:
Sub GetSheets()
Path = "C:\Users\hill\Desktop\Summary Doc Output Files\Summary Doc Output Files\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
Dim x As Integer
End Sub
but this only inserts new tabs and does not use the existing tab structure in place. 但这只会插入新的标签,而不会使用现有的标签结构。
Is there an easy way of doing this? 有一个简单的方法吗?
Since the 22 workbooks only have 1 sheet, you don't need to loop through each of those sheets. 由于22个工作簿只有1个工作表,因此您无需循环浏览每个工作表。 Also, you can just copy the contains of the sheet onto whichever sheet you desire in your Mainbook. 同样,您可以将表格的内容复制到您希望在Mainbook中使用的任何表格上。
so replace, 所以更换
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
With 同
ThisWorkbook.Sheets("Extract 1").UsedRange.Value = ActiveWorkbook.Sheets(1).UsedRange.Value
Note: The use of .UsedRange
assumes your data structure in each worksheet is the same as in the Extract
sheets and that there are no merged cells. 注意: .UsedRange
的使用假定每个工作表中的数据结构与“ Extract
表中的数据结构相同,并且没有合并的单元格。
And assuming you that copy the first workbook to Extract 1
sheet and so on you can place a counter in your macro to paste each workbook into a different sheet. 并假设您将第一个工作簿复制到“ Extract 1
张工作表”,依此类推,您可以在宏中放置一个计数器,将每个工作簿粘贴到另一张工作表中。
Sub GetSheets()
Dim x As Integer, wb as Workbook
Path = "C:\Users\hill\Desktop\Summary Doc Output Files\Summary Doc Output Files\"
Filename = Dir(Path & "*.xls")
x = 1
Do While Filename <> ""
Set wb = Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
ThisWorkbook.Sheets("Extract " & x).UsedRange.Value = wb.Sheets(1).UsedRange.Value
wb.Close false
x = x + 1
Filename = Dir()
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.