Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express cell id using string letter and current row number in an Excel formula?

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?

like image 479
Michael Avatar asked Jan 17 '13 16:01

Michael


People also ask

How do I find cell value based on row and column ID in Excel?

=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.

What is the column letter and row number that identify a cell?

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.

How do you reference a cell in a text string in Excel?

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”).


2 Answers

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.

like image 73
RocketDonkey Avatar answered Oct 14 '22 00:10

RocketDonkey


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)

like image 41
Bill the Lizard Avatar answered Oct 13 '22 23:10

Bill the Lizard