How to copy last 30 rows (Run-time error 1004)

sim89

New Member
Joined
Feb 23, 2018
Messages
3
Hi,
I'm trying to get a macro to copy the last 30 rows of data to a different sheet.

I have the code below but I am getting "Run-time error 1004: Application-defined or Object-defined error"

I've tried a few different things but can't work out what's causing the error.

Code:
Sub Last30()
Dim lastrow As Long

With Sheets("Stage1")
lastrow = Sheets("Stage1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Stage1").Range("A" & lastrow - 29 & ":S" & lastrow).Copy Sheets("Stage1Last30").Range("A2")
End With

With Sheets("Stage2")
lastrow = Sheets("Stage2").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Stage2").Range("A" & lastrow - 29 & ":AA" & lastrow).Copy Sheets("Stage2Last30").Range("A2")
End With

End Sub

Any help would be really appreciated.
Thanks
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hello and welcome.

On what line do you get the error?

Do all the following exist? Ensure the spelling is exactly right:

Sheets("Stage1")
Sheets("Stage1Last30")
Sheets("Stage2")
Sheets("Stage2Last30")

Also, ensure the sheets aren't protected.
 
Upvote 0
Hi gallen, thanks for your reply

I get the error on the line
Code:
Sheets("Stage1").Range("A" & lastrow - 29 & ":S" & lastrow).Copy Sheets("Stage1Last30").Range("A2")

I've checked the sheet names are spelled correctly, and none of the sheets are protected
 
Upvote 0
What is the value of lastrow when you get the error?
Also your With Statements are currently redundant, they should be like
Code:
With Sheets("Stage1")
   lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
   .Range("A" & lastrow - 29 & ":S" & lastrow).Copy Sheets("Stage1Last30").Range("A2")
End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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