VBA SUM with variable amount of cells

Strooprover

New Member
Joined
Jul 21, 2017
Messages
25
Hi all,
I want to put a sum formula in a cell but I want my formula to depend on the value of another cell. So for instance:

A regular sum formula with relative reference looks like this:
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"

However, I want to change the -2 value that I colored green into another variable.
For Instance.
For i = 1 to 50
Range("F" & i).Value

SUMVAL = Range("F" & i).Value
next i

And then I want to use the value of Range("F"&i) to decide how much offset the sum formula needs to use.

So = ActiveCell.FormulaR1C1 = "=SUM(R[sumval]C:R[-1]C)"

How can I accomplish this?

Thanks in advance!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Need to use concatenate...

Try

ActiveCell.FormulaR1C1 = "=SUM(R[" & sumval & "]C:R[-1]C)"
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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