Pasting into a named range and expanding the range

rob1987

New Member
Joined
Feb 14, 2011
Messages
39
Hi,

I have some data I want to copy from a range named "Benefitsinput" in worksheet "Sheet1" into a range named "Benefits" in worksheet "Templates". However, when I run my macro and paste this data into the last row in the "Benefits" range I want the range to expand to include this new row.

The Macro I have currently is:

Sub Copy_Range()
With Range("Benefits")
.Resize(.Rows.Count + 1, .Columns.Count).Name = "Benefits"
End With
Worksheets("Sheet1").Range("Benefitsinput").Cut Destination:=Range("Benefits").Cells(1, 1).Offset(Range("Benefits").Rows.Count, 0)
End Sub

This works for the first time I run my macro but the next time I try to run it with a new set of data in "Benefitsinput" I get a runtime error '1004': Application defined or object-defined error.

Please can anyone point out where I am going wrong?

Thank you
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You are cutting Benefitsinput, so it is now on sheet Templates rather than Sheet1. Try copying then clearing the contents.
 
Upvote 0
The problem is mainly with the cut and paste. When you cut a range and paste elsewhere, you transfer the range reference to the new destination. So, next time you look for range("BenefitsInput") it is no longer on Sheet1.
Try...
Code:
Sub AnotherCopy()
    Range("BenefitsInput").Copy _
        Destination:=Range("Benefits").End(xlDown).Offset(1, 0)
    Range("BenefitsInput").ClearContents
End Sub

{EDIT} Got sidetracked; Andrew beat me to the punch.

Denis
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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