LastRow Code Debug + Copy Formula in last row

khlau

New Member
Joined
Jan 22, 2018
Messages
13
Hello, wondering if someone can help me.

I have put together the below code but not sure why it isn't working. I am trying to copy my formulas in range B8:F8, to all the way down to the last row - this last row number is calculated from the data in another sheet.

So e.g. in the School Payment Request sheet there are 81 rows from the start of the data "C12". So I would want the formula in the sheet to copy to range B89:F89 etc.

Just a bit stumped, any help appreciated thanks!

Sub CommandButton1_Click()


Dim mylastRow As Long
mylastRow = Worksheets("School Payment Request").Range("C12" & Rows.Count).End(xlDown).Row
Range("B8:F8").AutoFill Destination:=Range("B9:F" & mylastRow), Type:=xlFillDefault
MsgBox "Imported"


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This isn't going to work:

Code:
mylastRow = Worksheets("School Payment Request").Range("C12" & Rows.Count).End(xlDown).Row

For example, if Rows.Count = 1048576 (as it is in my version of Excel) you have:

Code:
mylastRow = Worksheets("School Payment Request").Range("C121048576").End(xlDown).Row

You may well get an error on the above. Using your method, you should just remove the "& Rows.Count" piece to leave:

Code:
mylastRow = Worksheets("School Payment Request").Range("C12").End(xlDown).Row

WBD
 
Upvote 0
Thanks for getting back to me, WBD, have amended as suggested. However I still get a debug error stating that "Autofill method of Range class failed".
 
Upvote 0
Ah. You also need to change the target range to include the original selection I think:

Rich (BB code):
Range("B8:F8").AutoFill Destination:=Range("B8:F" & mylastRow), Type:=xlFillDefault

WBD
 
Upvote 0
doh! Thank you, literally been driving me crazy for last hour and a half!

Ah. You also need to change the target range to include the original selection I think:

Rich (BB code):
Range("B8:F8").AutoFill Destination:=Range("B8:F" & mylastRow), Type:=xlFillDefault

WBD
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,401
Members
448,893
Latest member
AtariBaby

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