简体   繁体   English

将数据从多个工作表复制到多个工作簿

[英]Copying data from multiple worksheets to multiple workbooks

I am attempting to copy data from multiple worksheets in an excel file to multiple files that have a template in them. 我正在尝试将数据从excel文件中的多个工作表复制到其中具有模板的多个文件中。 So one excel file has 1500 worksheets with unique names and there exist 1500 excel files with the same name as the worksheets. 因此,一个excel文件具有1500个具有唯一名称的工作表,并且存在1500个与工作表具有相同名称的excel文件。 I am trying to copy data (typically A1:A50) from each worksheet to another file of the same name. 我试图将每个工作表中的数据(通常为A1:A50)复制到同名的另一个文件中。 The target excel file has two worksheets in it and this data needs to go into each one: cells B5:B55 in "Inside Page", and cells C5:C55 in "Back Page." 目标excel文件中有两个工作表,每个数据都需要放入其中:“内页”中的单元格B5:B55和“后页”中的单元格C5:C55。

Any help would be much appreciated! 任何帮助将非常感激!

Lalitha 拉利萨

This should get you started. 这应该使您入门。 The only issue may be performance if you have 1500 (!) worksheets. 如果您有1500(!)个工作表,则唯一的问题可能是性能。

Option Explicit
Public Sub splitsheets()
    Dim srcwb As Workbook, trgwb As Workbook
    Dim ws As Worksheet, t1ws As Worksheet, t2ws As Worksheet
    Dim rng1 As Range, rng2 As Range
    Dim trgnm As String
    Dim fpath As String

    Application.ScreenUpdating = False
'--> Set this to the location of the target workbooks
    fpath = "H:/copytest/"

    Set srcwb = ThisWorkbook
    For Each ws In srcwb.Worksheets
        trgnm = ws.Name
'--> Change A1:B3 to the range to be copied to inside page
        Set rng1 = srcwb.Sheets(trgnm).Range("A1:B3")
'--> Change C4:D5 to the range to be copied to outside page
        Set rng2 = srcwb.Sheets(trgnm).Range("C4:D5")

        Set trgwb = Workbooks.Open(fpath & trgnm & ".xls")
        With trgwb
            Set t1ws = .Sheets("Inside Page")
            Set t2ws = .Sheets("Outside Page")
        End With
'--> Change A1:B3 to the range where you want to paste
        rng1.Copy t1ws.Range("A1:B3")
'--> Change C4:D5 to the range where you want to paste
        rng2.Copy t2ws.Range("C4:D5")
        trgwb.Close True
    Next
    Application.ScreenUpdating = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将工作表从多个工作簿复制到当前工作簿 - Copying worksheets from multiple workbooks into current workbook 将工作表从多个工作簿复制到另一个工作簿中的现有工作表 - Copying worksheets from multiple workbooks into existing worksheets in a different workbook Excel VBA; 从不同位置的多个工作簿中复制特定的工作表 - Excel VBA; copying specific worksheets from multiple workbooks in different locations 将数据从多个工作簿复制到另一个工作簿 - Copying data from multiple workbooks to another 当某些工作簿有一张工作表,一些工作簿有很多,有些工作簿具有隐藏的工作表时,将工作表从多个工作簿复制到当前工作簿中 - Copying worksheets from multiple workbooks into current workbook when some workbooks have one sheet, some have many, some have hidden worksheets 将合并数据从多个工作表复制到.ActiveWorksheet - Copying Consolidating Data from Multiple Worksheets into .ActiveWorksheet 从多个工作簿中复制具有不同列和工作表名称的数据-VBA - Copying data with different columns and sheet name from multiple workbooks - VBA 将多个工作表中的Excel数据复制到一个工作表中 - Copying Excel data from multiple worksheets into one single sheet 将多个工作簿中的多个工作表中的数据复制到单个主工作簿中 - copy data from multiple worksheets in multiple workbooks, all into single master workbook VBA:从多个工作表复制粘贴多个工作簿 - VBA: Copy-Paste from multiple worksheets multiple workbooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM