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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
=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,215,378
Messages
6,124,604
Members
449,174
Latest member
ExcelfromGermany

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