Copy/Paste Macro Throwing an Error

rob51852

Board Regular
Joined
Jun 27, 2016
Messages
190
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I've put together a macro to copy/paste entire rows if the cell in column A contains a value. The aim is to have the macro paste in the first empty row in a sheet called Store. The problem is I keep getting an error - the debugger highlights the Rows.Count line.

Can anyone see what the issue is?

Thanks

VBA Code:
Sub CopyToStore()
    Dim c As Range
    Dim Source As Worksheet
    Dim Target As Worksheet

    Set Source = ThisWorkbook.Worksheets("Input")
    Set Target = ThisWorkbook.Worksheets("Store")

    For Each c In Source.Range("A8:A300")
        If c <> "" Then
           Source.Rows(c.Row).Copy
           Target.Range("A" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        End If
    Next c
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Good...
Using the "dot" assumes that an Object had set as the parent of the Rows property, and this is not the case in your code. The plain Rows will refer to the default parent, ie the active sheet

Bye
 
Upvote 0
It really should be Target.Rows.Count as Rows.Count could error on occasion if unlikely.
 
Upvote 0
Steve is correct.
For example you would have an error if your active worksheet has an xlsx/xlsm/xlsb (1 mill Rows) format but the workbook that contains the macro (ie the one you are working with) has an xls (65kRows) format.

Bye
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top