Summing variable number of rows

hercush

New Member
Joined
Jul 22, 2002
Messages
17
How can I set the formula of an active cell using variables? I have a spreadsheet that I want to do a variety of totals on (inserting a row in the proper place and "Total" and then summing the variable number of prior rows).

For example, I want to sum the following:
B3:B7. The column will remain constant, but the startrow and endrow will vary. (I call them, as variables, "startrow" and "rowvar".)

I've tried things such as
ActiveCell.Formula="=SUM(Range(startrow & ':' & rowvar))"
OR
ActiveCell.FormulaR1C1="=SUM(R[offset]C:R[rowvar]C)"

I can't seem to get it. Does anyone know how to do this?

Thanks.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You need to indicate to VBA that you are using the range address.

Try
ActiveCell.Formula="=SUM(Range(startrow.address, endrow.address))"

where startrow and endrow refer to the starting and ending cells in the range that you want to sum.
 
Upvote 0
hercush,

For your example the code would be:

ActiveCell.Formula = "=SUM(B" & startrow & ":B" & rowvar & ")"

All you want to do is concatenate the various pieces that are required to construct the cell formula.

have fun
 
Upvote 0
That did it! I had tried a little concatenation earlier, but didn't think it through to this extent.

Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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