gpt4 book ai didi

vba - 将复制粘贴 VBA 宏从逐行更改为批量复制粘贴

转载 作者:行者123 更新时间:2023-12-04 21:12:43 26 4
gpt4 key购买 nike

我目前有一个 VB 宏,可以将值从一张表复制到另一张表。然而,目前,VB 的编写方式是逐行执行,并且运行速度非常慢,因为它要经过几千行。我想知道如何最好地更改我的 VB 以进行批量复制粘贴以减少等待时间。代码是:

Sub copypaste_settlement_rows()

Dim LastRow As Long

Application.ScreenUpdating = False

Sheets("Settlement Template").Select
'find last row in column A
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For x = 2 To LastRow
Cells(x, 1).Resize(1, 42).Copy
Sheets("PIVOT DATA").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Settlement Template").Select
Next x

Sheets(">> START HERE <<").Select

Application.CutCopyMode = False

Application.ScreenUpdating = True

End Sub

最佳答案

这应该是即时的,并且不使用剪贴板:

Sub copypaste_settlement_rows()
Dim v
With Sheets("Settlement Template")
v = .Cells(2, 1).Resize(.Cells(.Rows.Count, "A").End(xlUp).Row, 42)
End With
With Sheets("PIVOT DATA")
.Cells(.Rows.Count, "A").End(xlUp).Resize(UBound(v), UBound(v, 2)) = v
End With
End Sub

关于vba - 将复制粘贴 VBA 宏从逐行更改为批量复制粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com