簡體   English   中英

將復制粘貼VBA宏從逐行更改為批量復制粘貼

[英]Change copy-paste VBA macro from line-by-line to batch copy-paste

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

一種非常簡單的方法(也是我自己的代碼中最快的方法)是:

ThisWorkbook.Worksheets("PIVOT DATA").Range("A2:A" & lastRow) = ThisWorkbook.Worksheets("Settlement Template").Range("A2:A" & lastRow).Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM