简体   繁体   English

加快跨多个工作表的复制/粘贴 VBA

[英]Speed up copy/pasting across multiple sheets VBA

I have managed to write code which runs but it's quite slow (takes about 2 minutes atm and the block size is going to increase by 52 soon).我设法编写了运行但速度很慢的代码(大约需要 2 分钟 atm 并且块大小很快会增加 52)。 I would like to know if there's a better way to achieve this small section of code.我想知道是否有更好的方法来实现这一小段代码。 The code selects 10 sheets simultaneously so that the copy and paste will affect all 10 sheets.代码同时选择 10 张,以便复制和粘贴将影响所有 10 张。 The only copy and paste that I could get to work was the absolute worst version, as you can see, because all the other options I tried only pasted the ranges to a single sheet, not all 10. Would love to know if it's possible to do better.如您所见,我唯一可以开始工作的复制和粘贴是绝对最差的版本,因为我尝试的所有其他选项都只将范围粘贴到一张纸上,而不是全部 10 个。很想知道是否有可能做得更好。 I had to activate the first sheet and do the full range.copy, range.select, activesheet.paste, varray contains 10 sheet names, MaxRngLive is 20. RngLiveRange is a 1 dimensional array of 156 columns containing live formulae and RngPasteRange is a 156x156 block into which those formulae will be pasted, Later in the code.我必须激活第一张工作表并执行完整的 range.copy、range.select、activesheet.paste、varray 包含 10 个工作表名称,MaxRngLive 是 20。RngLiveRange 是一个包含实时公式的 156 列的一维数组,RngPasteRange 是 156x156这些公式将被粘贴到的块中,稍后在代码中。 I value out all of the cells in each block except the top row (again using range,pastespecial xlpastevalues.), thus reducing file size by about 75% overall.我将除第一行之外的每个块中的所有单元格赋值(再次使用范围,粘贴特殊 xlpastevalues。),从而将文件大小总体减少约 75%。

Sheets(varray).Select

Sheets("c_DAZN.com_GF").Activate

Set ws2 = wb.Worksheets(varray(0))


For j = 1 To MaxRngLive
      
    Application.StatusBar = ws2.Name & " " & j
      
    Set RngLiveRange = ws2.Range(Range(RngLiveName).Rows(j))
    Set RngPasteRange = ws2.Range(Range(RngPasteName).Rows(j))
    RngLiveRange.Copy
    RngPasteRange.Select
    ActiveSheet.Paste
    
Next j

Thanks very much everyone!非常感谢大家!

This should do what you want.这应该做你想要的。 I'm assuming your list of destination sheets is a string我假设您的目标工作表列表是一个字符串

Function copyRange(sourceRange As Variant, destRange As String, wks as String)
    arr = Split(wks, ",")
    For Each i In arr
        Worksheets(i).Range(destRange) = sourceRange.Formula
    Next i
End Function

Sub test()
wk = "Sheet1,Sheet2,Sheet3" 'Comma separated list of destination sheets
'Tested by selecting the range to be copied, you can define your source range however you want
Call copyRange(Selection, Selection.Address, wk)
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM