我目前有一个 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/