VBA Formula with dynamic range.

leemcder

New Member
Joined
Feb 26, 2018
Messages
42
Hi, I'm struggling with a formula in VBA. I need a formula for a dynamic range. This is the formula I have in my spreadsheet =SUM(P10:Q16)/(SUM(C10:C16)-SUM(D10:16))

I need help with the code below, which I need to put in the macro. I want it to calculate the cells from row 5 to the last row. The code below doesn't work, but should hopefully show you roughly what I need? I need it to work like the formula above, but with a dynamic range. Can anyone please help?

With Sheets("RTA") 'put your sheet name here
lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
End With


*this post has also been placed on http://www.vbaexpress.com/forum/showthread.php?64478-VBA-Formula-with-dynamic-range-Please-help<strike>
</strike>
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
=SUM(P10:Q16)/(SUM(C10:C16)-SUM(D10:16))

The last SUM.. no ending column reference

Presumably this should be =SUM(P10:Q16)/(SUM(C10:C16)-SUM(D10:D16))
 
Last edited:
Upvote 0
Hi, I'm struggling with a formula in VBA. I need a formula for a dynamic range. This is the formula I have in my spreadsheet =SUM(P10:Q16)/(SUM(C10:C16)-SUM(D10:16))

I need help with the code below, which I need to put in the macro. I want it to calculate the cells from row 5 to the last row. The code below doesn't work, but should hopefully show you roughly what I need? I need it to work like the formula above, but with a dynamic range. Can anyone please help?

With Sheets("RTA") 'put your sheet name here
lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
End With


*this post has also been placed on http://www.vbaexpress.com/forum/showthread.php?64478-VBA-Formula-with-dynamic-range-Please-help<strike>
</strike>
Solved it myself - code below worked

Code:
With Sheets("RTA") 'put your sheet name here
    lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
End With[LEFT][COLOR=#222222][FONT=Tahoma]
[/FONT][/COLOR][/LEFT]
 
Last edited:
Upvote 0
Try using R1C1 notation.
Code:
With Sheets("RTA") 'put your sheet name here
    lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("U" & lLastRow + 2).FormulaR1C1 = "=Sum(R5C[-5]:R[-2]C[-4])/(Sum(R5C[-18]:R[-2]C[-18])-Sum(R5C[-18]:R[-2]C[-17]))"
End With
 
Upvote 0
Try using R1C1 notation.
Code:
With Sheets("RTA") 'put your sheet name here
    lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("U" & lLastRow + 2).FormulaR1C1 = "=Sum(R5C[-5]:R[-2]C[-4])/(Sum(R5C[-18]:R[-2]C[-18])-Sum(R5C[-18]:R[-2]C[-17]))"
End With

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

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