Get value from the cell above - reference

Is it possible in Google Spreadsheets to reach the value of the cell just above?
Meaning: In one cell A2 I want to display the value from the cell above, that is A1. Not because it is that coordinate, but because it is the cell above. I do this by simply setting the value equal to the above cell:
If I create a new row in between those two, I get this:
As we know, no change in the values, since the reference is not relative in this way. How can I make it relative to the cell, so I always pick the cell above nomatter what is changed? I want the result to be this:
The term relative is wrong in the case of course, but I hope you get my point. I both want the usual relative behavior (so I can move the cell itself and its reference will fit to the new coloumn and row) as well as the behavior that the reference does not point towards a specific fixed coordinate but rather a specific path from the cell itself.

You can address it using the following function:
=INDIRECT(ADDRESS(ROW()-1;COLUMN()))
COLUMN() returns a numeric reference to the current column
ROW() returns a numeric reference to the current row.
In the example here, subtracting 1 from the row gives you the previous row. This math can be applied to the ROW() and/or the COLUMN(), but in answering your question, this formula will reference the cell above.
Then we have ADDRESS() which accepts a numeric row and column reference and returns a cell reference as a string.
Finally INDIRECT() allows you to pass a cell reference in as a string, and it processes it as a formula.
Google Spreadsheets give you help hints as you type, so you should get a full explanation of each function as you type the formula above in.

For anyone who stumbles across this question, you can also specify the column by doing something like this:
=INDIRECT("A" & ROW()-1)
This comes in handy if you are returning values in Column B but checking against the previous row in Column A.

The shortest, and easier for VisiCal old timer is the old RC syntax with relative values…
=INDIRECT("R[-1]C[0]"; FALSE)
Very visual, simple code template to remember and modify, and very short.
Regards, Antoine

Related

EXCEL: When dragging cells to the right I need to use information stored in columns not rows (Not sure how to ask this properly)

It's probably a simple problem, but I did not even know the keywords to google it ;/. Let's say I have this data :
Now I also have this litle formula:
If I know drag the C cell to the right, Excel will attempt the following caluclation:
=2+B1
What I want him to do is to attempt this calculation
=2+A2
Of course the easiest solution would be to store my initial data in one row instead of 1 column, but it is really inconvenient for me. Thanks for any help
You can use the indirect() method to reference a cell by it's "String identifier", i.e. "A3". When filling out to the right, use CONCATENATE() and COLUMN() to create your String identifiers {A1,A2,A3,A4,A5...} as required:
=2+INDIRECT(CONCATENATE("A";COLUMN()-2))
This will result in the following:
Side-Node: If you want this for some x/y-Grid-Generation, you can also be lazy,
and just insert =COLUMN() for every cell from "A1 - Z1" and ROW() for every cell from "A2 - A24".
(Or even avoid these at all and directly perform your actual calculation by using column() and row() as replacement for your x/y.
You may try using a combination of the INDIRECT and COLUMN functions:
=2+INDIRECT("A"&(COLUMN()-2))
You would paste the above formula into cell C1, and then drag across to the right however many columns/rows you wanted to cover.
This would result in the following:
This works because COLUMN()-2 returns 1 for the C column, 2 for the D column, and so on. Therefore, the formula will be calling INDIRECT on A1, A2, etc. for column C, D, and so on.
In general, if you want relative references to move down as cells are dragged to the right, you can use this:
Instead of:
= 2+A1
Do:
= 2+INDEX($A:$A,COLUMN()+<offset>)
Where <offset> is whatever offset you need. The offset will change depending on which column the starting formula is located in.
INDEX should be preferred over INDIRECT because INDIRECT is volatile (must recalculate after any change to the workbook) but INDEX is not (only recalculated when one of the inputs the formula, in this case $A:$A, changes).

Why won't my spreadsheet equations stay the same after i add a new row

I am making a spreadsheet which has multiple sheets and one of the sheets has an equation that looks to see if there is anything written in a certain cell on all the other sheets. Right now I just use this equation to find out whether or not it has anything written in that cell.
=IF(LEN('A'!N18)>1,CONCATENATE('A'!C18," "),)
This is just checking to see if anything is in the cell. The problem is that I want this to keep working if I add a new row on A, but right now it bumps row 18 to 19 and I am left with no row 18 so it won't even check that new row!
If you want to add a row between row 1 and row 18 on worksheet A but keep your formula references to A!N18 and A!C18 then you need to use INDIRECT or INDEX. Of these two, INDEX is the better choice as it is not volatile. A volatile function like INDIRECT will recalculate whenever anything in the workbook chanmges. A non-volatile function like INDEX will only recalculate when something that affects its outcome is changed.
'INDIRECT method; works but not optimal
=IF(LEN(INDIRECT("'A'!N18"))>1,CONCATENATE(INDIRECT("'A'!C18"," "),)
'INDEX method; works and is non-volatile
=IF(LEN(INDEX('A'!N:N, 18))>1,CONCATENATE(INDEX('A'!C:C, 18)," "),)
The link that Slai posted in the comments to your question should be what you're looking for. If you don't want a reference to change when you add/delete rows/columns you need to use absolute references. As currently written "N18" and "C18" are relative references. If you change the columns/rows on sheet 'A', these references will automatically change with them. That's why it's moving to row 19 on you.
You can turn these into absolute references by adding "$" like this: "$C$18" and "$N$18". The first "$" sets the absolute reference for the column and the second sets the absolute reference for the row. You can mix and match these for various results.
Mixing relative and absolute values really comes in handy when you are reusing a formula with slight differences. For instance, if you want to multiply a number in column b (starting with row 4) by the number in A1 and show the result in column C (also starting with row 4). You'll always be using A1 so we can set this as an absolute value and in C4 enter the formula =$A$1*B4. Copy this down column C and it will automatically update the B value to the new column but will always use "$A$1" for the other part.
You can split the relative reference by only using the "$" on the column or row reference of the reference. Whichever one you use it on will be locked and the other can still adjust based on changes to the sheet or copying. Learning how to use absolute references can be a great time-saver.
I'm curious about part of your formula though. Why are you concatenating C18 with just a blank space? If you are using the info somewhere else, it may make sense to add the space in that concatenate but it's a personal choice.

Excel - How to use the value of a cell as the row value of another cell?

Here is my problem:
I have a cell (V4) containing the value 444. I want to use this value in the formula of another cell (M12) in the following way. I want the formula to be equivalent to =MIN(L12:L444) but instead of 444 I want to refer to cell V4 which contains the value 444. But when I type in =MIN(L12:L(V4)) it obviously doesnt work so how do I do it? Sorry if I didn't explain it very well. :S
Would this work for you:
=MIN(L12:INDIRECT("L"&$V$4))
From: Excel - INDIRECT and Using the value in a cell as a cell reference in a formula?
INDIRECT will work and is closest to the solution you described, but I prefer OFFSET, which uses proper references. (For example, if you insert a column in the sheet before L, INDIRECT will break while OFFSET will just update its reference as expected.
Two ways to go with OFFSET:
1 - Start at L$1 and go down $V$4-1 rows. (This will work with $V$4 as you've defined it now.)
=MIN(L12:OFFSET(L$1,$V$4-1,0))
2 - In $V$4, provide the height of the range you want.
=MIN(OFFSET(L12,0,0,$V$4,0))
It's hard to make suggestions without more context, but I'm sure you can tweak one of these patterns to meet your needs.
I am not sure if you are trying to include all of the values in 1 column and then on non-contiguous cell. If so, it should look like = Min(L12:L444,V4) . The L12:L444 looks at the value in every cell in the L column from 12 - 444.
So you can check individual cell (A3, D15, Q54) with commas, or a range of cells (A3:Z54) with a colon. Or a range and a specific cell like above =Min(L12:L444, V4).

Using the current SUMIF "row" in criteria

I am trying to do something like this:
=SUMIF(H2:H55,VLOOKUP(insert placeholder for current row,Interfaces_to_Devices,5,FALSE)=A4,J2:J55)
IE I am trying to use the current row that I am evaluating to do a VLOOKUP of a value that needs to be equal to something I specify.
Thank you.
In this screen shot I did not include the VLOOKUP()="A4". That is supposed to be in there.
ROW() will return the number of the current row.
But the first argument to VLOOKUP is a cell reference or value.
If you explain what value you are trying to use in the VLOOKUP, how it is related to the current cell, then it is probably possible to use INDEX and MATCH (perhaps OFFSET) to achieve what you need.
=SUMIF(H2:H55,VLOOKUP(ROW(),Interfaces_to_Devices,5,FALSE)=A4,J2:J55)
Or
=SUMIF(H2:H55,INDEX(Interfaces_to_Devices,5,ROW())=A4,J2:J55)
If you simply REFERENCE the first value in column 5 of Interfaces_to_Devices, then drag down your formula you will simply reference each value going down the column without the need for a lookup, index, or match.
Notice in the picture below how i replace your formula with the cell reference to the 5th column in the table and in my first Row i Reference the first value in the column then as i drag the formula down it automatically references the equivalent cells in each row down the column.

Excel Reference To Current Cell

How do I obtain a reference to the current cell?
For example, if I want to display the width of column A, I could use the following:
=CELL("width", A2)
However, I want the formula to be something like this:
=CELL("width", THIS_CELL)
Several years too late:
Just for completeness I want to give yet another answer:
First, go to Excel-Options -> Formulas and enable R1C1 references. Then use
=CELL("width", RC)
RC always refers the current Row, current Column, i.e. "this cell".
Rick Teachey's solution is basically a tweak to make the same possible in A1 reference style (see also GSerg's comment to Joey's answer and note his comment to Patrick McDonald's answer).
Cheers
:-)
Create a named formula called THIS_CELL
In the current worksheet, select cell A1 (this is important!)
Open Name Manager (Ctl+F3)
Click New...
Enter "THIS_CELL" (or just "THIS", which is my preference) into Name:
Enter the following formula into Refers to:
=!A1
NOTE: Be sure cell A1 is selected. This formula is relative to the ActiveCell.
Under Scope: select Workbook.
Click OK and close the Name Manager
Use the formula in the worksheet exactly as you wanted
=CELL("width",THIS_CELL)
EDIT: Better solution than using INDIRECT()
It's worth noting that the solution I've given should be preferred over any solution using the INDIRECT() function for two reasons:
It is nonvolatile, while INDIRECT() is a volatile Excel function, and as a result will dramatically slow down workbook calculation when it is used a lot.
It is much simpler, and does not require converting an address (in the form of ROW() COLUMN()) to a range reference to an address and back to a range reference again.
EDIT: Also see this question for more information on workbook-scoped, sheet dependent named ranges.
EDIT: Also see #imix's answer below for a variation on this idea (using RC style references). In that case, you could use =!RC for the THIS_CELL named range formula, or just use RC directly.
You could use
=CELL("width", INDIRECT(ADDRESS(ROW(), COLUMN())))
=ADDRESS(ROW(),COLUMN(),4) will give us the relative address of the current cell.
=INDIRECT(ADDRESS(ROW(),COLUMN()-1,4)) will give us the contents of the cell left of the current cell
=INDIRECT(ADDRESS(ROW()-1,COLUMN(),4)) will give us the contents of the cell above the current cell (great for calculating running totals)
Using CELL() function returns information about the last cell that was changed. So, if we enter a new row or column the CELL() reference will be affected and will not be the current cell's any longer.
A2 is already a relative reference and will change when you move the cell or copy the formula.
=ADDRESS(ROW(),COLUMN())
=ADDRESS(ROW(),COLUMN(),1)
=ADDRESS(ROW(),COLUMN(),2)
=ADDRESS(ROW(),COLUMN(),3)
=ADDRESS(ROW(),COLUMN(),4)
Without INDIRECT(): =CELL("width", OFFSET($A$1,ROW()-1,COLUMN()-1) )
I found the best way to handle this (for me) is to use the following:
Dim MyString as String
MyString = Application.ThisCell.Address
Range(MyString).Select
Hope this helps.
Inside tables you can use [#] which (unfortunately) Excel automatically expands to Table1[#] but it does work. (I'm using Excel 2010)
For example when having two columns [Change] and [Balance], putting this in the [Balance] column:
=OFFSET([#], -1, 0) + [Change]
Note of course that this depends on the order of the rows (just like most any other solution), so it's a bit fragile.
There is a better way that is safer and will not slow down your application. How Excel is set up, a cell can have either a value or a formula; the formula can not refer to its own cell. You end up with an infinite loop, since the new value would cause another calculation... . Use a helper column to calculate the value based on what you put in the other cell. For Example:
Column A is a True or False, Column B contains a monetary value, Column C contains the folowing formula:
=B1
Now, to calculate that column B will be highlighted yellow in a conditional format only if Column A is True and Column B is greater than Zero...
=AND(A1=True,C1>0)
You can then choose to hide column C
Full credit to the top answer by #rick-teachey, but you can extend that approach to work with Conditional Formatting. So that this answer is complete, I will duplicate Rick's answer in summary form and then extend it:
Select cell A1 in any worksheet.
Create a Named Range called THIS and set the Refers to: to =!A1.
Attempting to use THIS in Conditional Formatting formulas will result in the error:
You may not use references to other workbooks for Conditional Formatting criteria
If you want THIS to work in Conditional Formatting formulas:
Create another Named Range called THIS_CF and set the Refers to: to =THIS.
You can now use THIS_CF to refer to the current cell in Conditional Formatting formulas.
You can also use this approach to create other relative Named Ranges, such as THIS_COLUMN, THIS_ROW, ROW_ABOVE, COLUMN_LEFT, etc.
EDIT: the following is wrong, because Cell("width") returns the width of the last modified cell.
Cell("width") returns the width of the current cell, so you don't need a reference to the current cell. If you need one, though, cell("address") returns the address of the current cell, so if you need a reference to the current cell, use indirect(cell("address")). See the documentation: http://www.techonthenet.com/excel/formulas/cell.php
Reference to a cell that include this formula (self reference):
address(row();column())
E.g. getting the value of the cell above:
indirect(address(row()-1;column()))
Or what the OP asked:
=Cell(width;address(row();column()))

Resources