简体   繁体   English

将工作表从多个工作簿复制到另一个工作簿中的现有工作表

[英]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.

相关问题 将多个工作簿中的工作表复制到一个工作簿中并粘贴到右侧 - Copying worksheets from multiple workbooks into one workbook pasting to the right 将数据从工作簿中的多个工作表复制到单独工作簿中的不同工作表-VBA Excel - Copying Data from Multiple Worksheets in Workbooks to Differing Worksheets in Separate Workbook - VBA Excel Excel VBA; 从不同位置的多个工作簿中复制特定的工作表 - Excel VBA; copying specific worksheets from multiple workbooks in different locations 将数据从多个工作表复制到多个工作簿 - Copying data from multiple worksheets to multiple workbooks 当某些工作簿有一张工作表,一些工作簿有很多,有些工作簿具有隐藏的工作表时,将工作表从多个工作簿复制到当前工作簿中 - Copying worksheets from multiple workbooks into current workbook when some workbooks have one sheet, some have many, some have hidden worksheets 将各种工作簿和工作表中的值复制到其他工作簿中 - Copying values from various Workbooks and Worksheets into other Workbook 将不同工作簿中工作表中的值复制到现有工作表 - Copy Values from Worksheets in a different Workbook to Existing Worksheets 将位于同一文件夹中的多个工作簿中的数据复制并粘贴到预先存在的工作簿的多个工作表中 - Copy and paste data from multiple workbooks located in the same folder to several worksheets of a pre-existing workbook 将来自多个工作簿的数据与多个工作表合并到摘要工作簿中 - combining data from multiple workbooks with multiple worksheets into summary workbook 如何将工作表从多个工作簿合并到新工作簿中 - How to merge Worksheets from Multiple Workbooks into New WorkBook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM