【问题标题】:Can't Cut and Paste data from one sheet to another无法将数据从一张表剪切和粘贴到另一张表
【发布时间】:2019-10-22 14:04:00
【问题描述】:

我正在尝试从所有行中剪切数据,直到“摘要”表中的最后一行,并将其粘贴到“历史”表中的下一个空白行,但我收到此错误: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

提前致谢!

【问题讨论】:

  • 我认为您以错误的方式使用函数PasteSpecial。如果您剪切数据,则不能使用 pastespecial。您应该首先复制数据和PasteSpecial 的值,然后您可以删除相同的范围。
  • Range("A2:V" & LastRow.

标签: excel vba


【解决方案1】:

这应该为您完成工作:

首先,我们复制范围和粘贴值,在下一步中,我们将清除从中复制它的范围。

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

【讨论】:

  • 非常感谢,这完全可以为我工作!干杯,祝你有美好的一天。 :)
猜你喜欢
  • 2015-02-09
  • 2019-10-10
  • 1970-01-01
  • 2020-01-13
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
相关资源
最近更新 更多