无法将数据从一张纸剪切并粘贴到另一张纸

时间:2019-06-07 04:28:05

标签: excel vba

我正在尝试从所有行中剪切数据,直到工作表“摘要”中的最后一个数据,然后将其粘贴到“历史记录”工作表中的下一个空白行,但是我收到此错误:PasteSpecial方法或Range类失败。

Sub HistoricalData()

  Application.ScreenUpdating = False
  Dim copySheet As Worksheet
  Dim pasteSheet As Worksheet
  Dim LastRow As Integer

  LastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).row

  Set copySheet = Worksheets("Summary")
  Set pasteSheet = Worksheets("Historical")

  copySheet.Range("A2:V2" & LastRow).Cut
  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
  Application.ScreenUpdating = True

End Sub

提前谢谢!

1 个答案:

答案 0 :(得分:0)

这应该为您完成工作:

首先,我们要复制“范围和粘贴值”,在下一步中,我们从复制它的位置清除该范围。

Sub HistoricalData()

  Application.ScreenUpdating = False
  Dim copySheet As Worksheet
  Dim pasteSheet As Worksheet
  Dim LastRow As Integer

  LastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row

  Set copySheet = Worksheets("Summary")
  Set pasteSheet = Worksheets("Historical")

  copySheet.Range("A2:V" & LastRow).Copy
  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  copySheet.Range("A2:V" & LastRow).Clear

  Application.CutCopyMode = False
  Application.ScreenUpdating = True

End Sub