0

In my sheet I calculate the prime factors for a number, the range is from E6 onwards. I have the following line of code to determine the number of rows.

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

The minus 1 is to exclude the header and get only the range containing numbers. I have also set the range and copied as follows:

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

However, when I try to paste this in a cell it gives me the following error:

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

Here's the coding I have so far

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)

Any assistance would be appreciated.

Baber Beg
  • 1
  • 1
  • 3
  • Hi, there's been countless copy paste range threads here (and everywhere else on the internet...) on daily basis. Please use the search function! – Samuel Hulla Jun 22 '18 at 11:13
  • Duplicate of [VBA paste range](https://stackoverflow.com/questions/18968856/vba-paste-range) (and many more). For example I recently [answered one here](https://stackoverflow.com/a/50794565/5512705) This also includes few useful mentions as to why you should avoid using ActiveCell and so on. The copy-paste method seems to be fairly similar to what you want to achieve – Samuel Hulla Jun 22 '18 at 11:14
  • So to clarify: You want to copy data from `column E` starting from `row 6` to the last used row. Then paste this 5 rows below the last used row in `column D`. Is that correct? – DirtyDeffy Jun 22 '18 at 13:26
  • Hi DirtyDeffy, yep that's correct – Baber Beg Jun 22 '18 at 14:25

1 Answers1

1

This works:

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
DirtyDeffy
  • 497
  • 7
  • 18