首页 文章

Excel VBA复制和粘贴范围

提问于
浏览
0

在我的表格中,我计算了数字的素因子,范围从E6开始 . 我有以下代码行来确定行数 .

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

减1是排除 Headers 并仅获取包含数字的范围 . 我也设置了范围并复制如下:

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)

任何援助将不胜感激 .

1 回答

  • 0

    这有效:

    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
    

相关问题