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