【问题标题】:Excel VBA Copy and paste rangeExcel VBA 复制和粘贴范围
【发布时间】:2020-09-04 09:04:05
【问题描述】:

在我的工作表中,我计算了一个数字的质因数,范围从 E6 开始。我有以下代码行来确定行数。

rCount = Application.WorksheetFunction.CountA(Range("E:E")) - 1

负1是排除header,只获取包含数字的范围。我还设置了范围并复制如下:

Set rgCopy = Range("E6", Selection.End(xlDown))
    Selection.Copy

但是,当我尝试将其粘贴到单元格中时,会出现以下错误:

Run-time error '1004':
Method 'Range' of object'_Global' failed

这是我目前的编码

Dim rgCopy As Range
    Dim rCount As Integer

    Set rgCopy = Range("E6", Selection.End(xlDown))
    Selection.Copy
    rCount = Application.WorksheetFunction.CountA(Range("E:E")) - 1
    lastrow = Cells(Rows.Count, 4).End(xlUp).Row

    Range("E6:" & Selection.End(xlDown)).Copy Destination:=Range("D" & lastrow + 5)

任何帮助将不胜感激。

【问题讨论】:

  • 嗨,这里(以及互联网上的其他任何地方......)每天都有无数的复制粘贴范围线程。请使用搜索功能!
  • VBA paste range 的副本(以及更多)。例如我最近answered one here 这也包括一些有用的提及,为什么你应该避免使用 ActiveCell 等等。复制粘贴方法似乎与您想要实现的目标非常相似
  • 所以澄清一下:您想将数据从column E 开始从row 6 复制到最后使用的行。然后将这 5 行粘贴到 column D 中最后使用的行下方。对吗?
  • 嗨 DirtyDeffy,是的,没错

标签: vba excel


【解决方案1】:

这行得通:

Sub test()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row

ActiveSheet.Range("E6:E" & LastRow).Copy

LastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row

ActiveSheet.Range("D" & LastRow + 5).PasteSpecial xlPasteAll
Application.ScreenUpdating = True
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多