46
votes

How to create a Google Spreadsheet sum() which always ends on the cell above, even when new cells are added? I have several such calculations to make on each single column so solutions like this won't help.

Example:

On column B, I have several dynamic ranges which has to be summed. B1..B9 should be summed on B10, and B11..B19 should be summed on B20. I have tens such calculations to make. Every now and then, I add rows below the last summed row , and I want them to be added to the sum. I add a new row (call it 9.1) before row 10, and a new raw (let's call it 19.1) before row 20. I want B10 to contain the sum of B1 through B9.1 and B20 to contain the sum of B11:B19.1.

On excel, I have the offset function which does it like charm. But how to do it with google spreadsheet? I tried to use formulas like this:

=SUM(B1:INDIRECT(address(row()-1,column(),false)))   # Formula on B10
=SUM(B11:INDIRECT(address(row()-1,column(),false)))  # Formula on B20

But on Google Spreadsheet, all it gives is a #name error.

I wasted hours trying to find a solution, maybe someone can calp? Please advise

Amnon

8
What the actual? Why is this so hard!?Ian Grainger

8 Answers

69
votes

You are probably looking for formula like:

=SUM(INDIRECT("B1:"&ADDRESS(ROW()-1,COLUMN(),4)))

Google Spreadsheet INDIRECT returns reference to a cell or area, while - from what I recall - Excel INDIRECT returns always reference to a cell. Given Google's INDIRECT indeed has some hard time when you try to use it inside SUM as cell reference, what you want is to feed SUM with whole range to be summed up in e.g. a1 notation: "B1:BX".

You get the address you want in the same way as in EXCEL (note "4" here for row/column relative, by default Google INDIRECT returns absolute):

ADDRESS(ROW()-1,COLUMN(),4)

and than use it to prepare range string for SUM function by concatenating with starting cell.

"B1:"&

and wrap it up with INDIRECT, which will return area to be sum up.

REFERRING TO BELOW ANSWER from Druvision (I cant comment yet, I didn't want to multiply answers)

Instead of time consuming formulas corrections each time row is inserted/deleted to make all look like:

=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))

You can spare one column in separate sheet for holding variables (let's name it "def"), let's say Z, to define starting points e.g. in Z1 write "B1" in Z2 write "B11" etc. and than use it as variable in your sum by using INDEX:

SUM(INDIRECT(INDEX(def!Z:Z,1,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B1 to calculated row, since in Z1 we have "B1" ( the 1,1 in INDEX(...,1,1) )

SUM(INDIRECT(INDEX(def!Z:Z,2,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B11 to calculated row, since in Z2 we have "B11" ( the 2,1 in INDEX(...,2,1) )

please note:

  1. Separate sheet named 'def' - you don't want row insert/delete influence that data, thus keep it on side. Useful for adding some validation lists, other stuff you need in your formulas.

  2. "Z:Z" notation - whole column. You said you had a lot of such formulas ;)

Thus you preserve flexibility of defining starting cell for each of your formulas, which is not influenced by calculation sheet changes.

By the way, wouldn't it be easier to write custom function/script summing up all rows above cell? If you feel like javascripting, from what I recall, google spreadsheet has now nice script editor. You can make a function called e.g. sumRowsAboveMe() and than just use it in your sheet like =sumRowsAboveMe() in sheet cell.

Note: you might have to replace commas by semicolons

5
votes

NOTE After testing this answer, it will only work if the sum is in a different column due to a circular dependency error. Otherwise, the solution is valid.

It's a bit of algebra, but we can take advantage of Spreadsheets' lower right corner drag.

=SUM(X:X) - SUM(X2:X)

Where X is the column you are working with and X2 is your ending point. Drag the formula down and Sheets will increment the X2, thus changing the ending point.

*You mentioned that you had tens of such calculations to make. So in order to fit your exact need, we would subtract your last summation to get that "middle" range that we wanted.

e.g.

B1..B9 should be summed on B10, and B11..B19 should be summed on B20

Because of the circular dependency error mentioned earlier, I can't solve it exactly and put the sum on the same line, but this could work in other cases where the sum needs to be stored in a different column.

=SUM(B:B) - SUM(B9:B) //Formula on C10 (Sum of B1..B9)
=SUM(B:B) - SUM(B19:B) - B10 // Formula on C20 (Sum of B11..B19)
2
votes

This is based on @PsychoFish, here is the solution:

=SUM(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"3:"&ADDRESS(ROW()-1,COLUMN(),4)))

Simply replace the "3:" for the row to start sum.

@PsychoFish is correct but cannot be dragged and copied since the column is literal and hard coded, and @Druvision was in the right direction but was wrong... basically ended up with the same issue of having to re-enter the ranges and then sliding the formulas over and over.

1
votes

General syntax:

=SUM(INDIRECT(cell_reference_as_string1 &":"& cell_reference_as_string2)

with for example:

cell_reference_as_string1 = ADDRESS(ROW(),COLUMN(),4)
cell_reference_as_string2 = ADDRESS(ROW()-1,COLUMN(),4)
1
votes

You guys are making this harder than you have to. I just leave a couple of empty rows above by "sum" row (you can format them to be filled with color or something to keep them from being inadvertently used), then just add your new rows just above those special rows.

1
votes

I like how @abernier describes the general solution. So far only alphabet-based A1 notation (A being first column, 1 being first row) are being used. It keeps confusing me, especially when thinking of number of columns left of another column. I like the number-based R1C1 notation much better. To use R1C1 notation for INDIRECT, you need to pass FALSE like so:

=SUM(INDIRECT("R1C"&COLUMN()&":R"&(ROW()-1)&"C"&COLUMN(), FALSE))

I hope you find that helpful, too.

1
votes

Agree with what user7255446 said that everyone is overcomplicating. Keep one row blank before your sum row. And then whenever you want to insert a new row, click on your blank row and use "Insert row ABOVE" instead of "insert row below". Your sum formula will automatically adjust. Example: I want to sum from B1 to B19. I leave row 20 blank. In cell B21, put =SUM(B1:B20). Then if you ever need to insert a new row, click on row 20 and choose "Insert row above". The sum formula automatically changes to =SUM(B1:B21) for you. And of course your sum cell is now B22.

-1
votes

The answer by @PsychoFish led me in the correct way. The only issue that I had to rewrite the formula again from each column and each sum. So here is the improved formula, which sums the previous 9 cells on the same column, without hardcoding the column or row numbers:

=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))

The only issue is that I had to rewrite the formulas if someone adds or deletes a row. In this case I should change 9 to 10 or 8 corrspondingly.