Bobilly

New Member
Joined
Mar 29, 2019
Messages
16
Hi,

I created a script to created three separate equations in columns, P, Q and R. I have it working up to autofilling down for P and Q, but am now getting a Run time 1004 error when it tries to auto fill down from R2 to wherever the bottom would be. Any suggestions would be helpful!


Sub CalculateMarginNrMPercent()


' Calculates Margin $, Net Realization and Margin %

Dim lastRow As Long

Range("P2").Select
ActiveCell.FormulaR1C1 = "=RC[-3]-(RC[-2]+RC[-1])"
lastRow = Range("O" & Rows.Count).End(xlUp).Row
Range("P2").AutoFill Destination:=Range("P2:P" & lastRow)

'Net Realization

Range("Q2").Select
ActiveCell.FormulaR1C1 = "=(RC[-5]+RC[-4])/RC[-7]"
lastRow = Range("P" & Rows.Count).End(xlUp).Row
Range("Q2").AutoFill Destination:=Range("Q2:Q" & lastRow)

'Margin %

Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-5]-(RC[-4]+RC[-3])/RC[-5]"
lastRow = Range("R" & Rows.Count).End(xlUp).Row
Selection.AutoFill Destination:=Range("R2:R" & lastLiveRow), Type:=xlFillDefault

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What's the error message?
 
Upvote 0
Try changing this
Code:
lastRow = Range("[COLOR=#ff0000]M[/COLOR]" & Rows.Count).End(xlUp).Row
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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