我目前有一个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发布于 2015-10-17 01:40:28
这应该是即时的,并且不使用剪贴板:
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发布于 2015-10-17 01:41:57
一种非常简单的方法(也是我在自己的代码中见过的最快的)是:
ThisWorkbook.Worksheets("PIVOT DATA").Range("A2:A" & lastRow) = ThisWorkbook.Worksheets("Settlement Template").Range("A2:A" & lastRow).Valuehttps://stackoverflow.com/questions/33176245
复制相似问题