I want to calculate the sum on a column and then subtract sum on another column BUT using only the values from a given row to the current row (the one in which formula resides).
So, in an "informal custom language", I would need something like this:
Suppose I am in C5: =(sum(A1:"A"+ROW())-sum(B1:"B"+ROW()))
How can I write a correct expression in Excel for this?
=INDEX() returns the value of a cell in a table based on the column and row number. =MATCH() returns the position of a cell in a row or column. Combined, the two formulas can look up and return the value of a cell in a table based on vertical and horizontal criteria.
By default, Excel uses the A1 reference style, which refers to columns as letters (A through IV, for a total of 256 columns), and refers to rows as numbers (1 through 65,536). These letters and numbers are called row and column headings. To refer to a cell, type the column letter followed by the row number.
Usually you type =A1 for referring to the cell A1 in Excel. But instead, there is also another method: You could use the INDIRECT formula. The formula returns the reference given in a text. So instead of directly linking to =A1, you could say =INDIRECT(“A1”).
You can try using INDIRECT
, which accepts a string reference to a range and returns the range itself:
=SUM(INDIRECT("A1:A"&ROW()))-SUM(INDIRECT("B1:B"&ROW()))
Here, we start with a 'stub' of "A1:A"
. We then get the current row with ROW()
(so 5 in this example) and concatenate it with our stub, giving us INDIRECT("A1:A5")
. Since INDIRECT
will return the range referenced by its argument ("A1:A5"
here), we can wrap it with the SUM
formula to get the result (which is identical to SUM(A1:A5)
). We then do the same thing for column B
.
I think you may be looking at it backwards. You need to anchor the first cell reference in the call to SUM
to the first row, but let the second cell reference change with the row. Try this in cell C1:
=SUM(A$1:A1) - SUM(B$1:B1)
Now when you copy that down the column, it becomes:
C2: =SUM(A$1:A2) - SUM(B$1:B2)
C3: =SUM(A$1:A3) - SUM(B$1:B3)
C4: =SUM(A$1:A4) - SUM(B$1:B4)
C5: =SUM(A$1:A5) - SUM(B$1:B5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With