加快在VBA中的复制和粘贴

时间:2019-07-08 13:57:14

标签: excel vba

我正在尝试通过复制和粘贴来修复1600行数据库,但是下面的代码很慢。关于如何加快速度的任何想法?

Sub copy()
    Dim ws As Worksheet
    Dim i As Long

    Application.ScreenUpdating = False
    Set ws = Sheets("UK File Cash Mod")

    For i = 2 To 1600
        If ws.Cells(i, 1) Like "384*" And Cells(i + 1, 1) = "" Then
            ws.Cells(i, 1).copy
            If ws.Cells(i + 1, 3) <> "" Then
                ws.Cells(i + 1, 1).PasteSpecial
                ws.Cells(i, 2).copy
                ws.Cells(i + 1, 2).PasteSpecial
            End If
        Else
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:2)

我通过一个简单的Destination.Value=Source.Value删除了所有复制粘贴,所以您得到了:

Sub copy()
  Dim ws As Worksheet
  Dim i As Long

  Application.ScreenUpdating = False

  Set ws = Sheets("UK File Cash Mod")

  For i = 2 To 1600
    If ws.Cells(i, 1) Like "384*" And Cells(i + 1, 1) = "" Then
      If ws.Cells(i + 1, 3) <> "" Then
        ws.Cells(i + 1, 1).Value = ws.Cells(i, 1).Value
        ws.Cells(i + 1, 2).Value = ws.Cells(i, 2).Value
      End If
    Else
    End If
  Next i
  Application.ScreenUpdating = True
End Sub