SUM Numbers Above

Vampy99

New Member
Joined
Jul 13, 2011
Messages
42
Hi,

I've wrote some VB code that displays a variation of numbers dependent on what is entered. The amount of rows there will be will differ because of this.

My question is simply does anyone know a formula or any code that SUMS everything above it? e.g

=SUM(A2:AX)

or perhaps

=SUM(A2:A"one_above_me")

A2 is constant. AX is the cell above where the formula is.

Hope this makes sense.

Luke
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Oops... this works great in Excel, but I cannot use this in VB due to their being 4 sets of quotation marks.

I have the following line of code:
Range(cellrefb & total).Formula = "=Sum(indirect("b2:b"& Row() - 1))"

cellrefb and total are both variables to put the formula in the correct cell.

Any ideas as to how to use this in VB, getting around the quotation marks problem?

Thanks,

Luke
 
Upvote 0
Oops... this works great in Excel, but I cannot use this in VB due to their being 4 sets of quotation marks.

I have the following line of code:


cellrefb and total are both variables to put the formula in the correct cell.

Any ideas as to how to use this in VB, getting around the quotation marks problem?

Thanks,

Luke
You need double quotes like this:

Range(cellrefb & total).Formula = "=Sum(indirect(""b2:b""& Row() - 1))"
 
Upvote 0
I usually prefer to parse it out with either CHR(34) or """" (four double quotes).

Code:
Range(cellrefb & total).Formula = "=Sum(indirect(" & Chr(34) & "B2:B" & Chr(34) & "&Row()-1))"
 
Upvote 0
Thank you T. Valko, that gives me exactly what I need.

I'm still relatively new to VB, so the tips you have given me will hold me in good stead. Thanks again T.Valko & Oaktree,

Luke
 
Upvote 0
Or

Code:
Cells(total, cellrefb).FormulaR1C1 = "=sum(r2c:r[-1]c)"

... which is non-volatile.

Or you can select A2, and do Insert > Name > Define, relN (as in "relative north") refers to =A1 (no $ signs), then use the formula

Code:
Cells(total, cellrefb).FormulaR1C1 = "=sum(r2c:relN)"

... which ALWAYS refers to the cell above, even if you insert rows.
 
Last edited:
Upvote 0
Thank you T. Valko, that gives me exactly what I need.

I'm still relatively new to VB, so the tips you have given me will hold me in good stead. Thanks again T.Valko & Oaktree,

Luke
You're welcome. Thanks for the feedback! :cool:
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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