Excel Reference To Current Cell - excel

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()))

Related

Special character wanted which prevents an Excel formula from changing its reference when the referenced cell is moved

With the special character '$' I can prevent an Excel formula from changing its reference when the cell itself is copied (e.g '$A$1').
But is there also a special character that prevents an Excel formula from changing its reference when the referenced cell is moved?
Here's an example:
I have:
Now I move the cells 'A1:A3' down one row. So the formulas in the cells 'C1:C3' change:
But I don't want that the formulas in the cells 'C1:C3' change. They should remain unchanged:
Try formula:
=IF(INDEX(A:A,ROW(1:1))=INDEX(B:B,ROW(1:1)),"Same","Not same")
the formula always work, regardless of any column been delete besides columns A or B
If you always want to refer to A1, then you can't just use A1 as a reference, because, as you have seen, Excel will adjust that reference if rows are inserted above.
Instead, use Index(A:A,1) . That will always return the value from the first row of column A.
=if(index(A:A,1)=index(B:B,1),"same","not")
Edit after comment: If this does not suit your purpose, maybe you need to redesign your spreadsheet so that the position of things does not change all the time. This looks like you are adding new data at the top of a list.
Excel works from top to bottom and things will be a lot easier if new data is added at the bottom of a list. You can always use other Excel tools to change the sort order if you want to have the data sorted in reverse chronological order.
Use formula in C1:
=IF(INDEX(A:A,ROW(ZZ1))=INDEX(B:B,ROW(ZZ1)),"Same","Not same")
and copy down.

Indirect Function Excel

I am rather new to excel formulas. I am using the Indirect function and it is working properly when I manually enter the formula into the data validation source. What I would like to do is to copy it down the entire row. I am currently using the formula: =INDIRECT($Y$2) Referencing Column (Y) and row (2).
After some research, it appears that I can use the row function to accomplish this, but I am unsure how to include that into the formula I already have.
The issue that the OP describes is that their formulas break when they duplicate the entire row where an indirect reference is made using =INDIRECT($Y$2) in the data validation source of a cell.
The problem has to do with the cell reference and how the "$" symbol is used. It is described in more detail here. However, the relevant information is that the symbol allows you to control how the referencing cell should behave toward the referenced cell. You can allow or prevent change to the relative positions between the two. If you want to copy/paste a referring cell you must decide if the referenced cell will be fixed or moved in the process.
fix the row and allow the column to change
=INDIRECT($Y2)
allow the row to change and fix the column
=INDIRECT(Y$2)
fix(prevent change) to both column and row
=INDIRECT($Y$2)
allow change to both column an row
=INDIRECT(Y2)
You can put a conditional IF function into the data validation source itself. Would that be sufficient?
You will have the static choices:
=$M$2:$M$4
And then the variable sets of choices:
=IF(A2="choice1",$Y$2:$Y$4,$Z$2:$Z$4)
Note that the first cell reference in the IF statement is relative, but all others are absolute.
To use the row the formula is on, you can use =Indirect("Y"&row())

How to reference cell within '' worksheet title

I have the names of the tabs/worksheets (M-61,M-62,M-63W) at the top row (A1, B1, C1...etc)
I am trying to get a sum of several cells within the different sheets:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
However, right now I’m referring to the sheet itself, and have to apply the same formula to all the other sheets. This will require me to manually go and change all the sheet titles accordingly.
I was wondering if there is any way to reference the top row with the sheet titles within the formula so it automatically refers to the row text instead of me having to manually change the sheet title.
Edit
Now i got the reference to work, just wondering how would I do a sum of several cells in that tab
=INDIRECT("'"&$F1&"'!H87",TRUE)
Maybe:
=SUM(INDIRECT("'"&C1&"'!H21"),INDIRECT("'"&C1&"'!H43"),INDIRECT("'"&C1&"'!H86:H87"),INDIRECT("'"&C1&"'!H97:H98"))
(though there may well be a much smarter way).
You can use the INDIRECT function, which uses a string as an argument and converts it to a range. So
=M-60'!H21
is the same as
=INDIRECT("M-60'!H21")
or, if Sheet name is stored in, say, cell C1:
=INDIRECT(C1&"'!H21")
Your example has a SUM, though, which requires some adaptations. This your example:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
Since you are not using a range, you can convert that SUM into simple addition. Assuming Sheet name in cell C1
=INDIRECT("'"&C1&"'!H21")+INDIRECT("'"&C1&"'!H43")+INDIRECT("'"&C1&"'!H86")+INDIRECT("'"&C1&"'!H87")+INDIRECT("'"&C1&"'!H97")+INDIRECT("'"&C1&"'!H98")
This should solve your problem. More info here
By the way, if you were using a range, the OFFSET function with INDIRECT as an argument would work. But that's not necessary here.

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

Excel formula to reference 'CELL TO THE LEFT'

I'm trying to do conditional formatting so that the cell color will change if the value is different from the value in the cell left of it (each column is a month, in each row are the expenses on certain object. I want to monitor easily changes in prices over months.)
I can do it per cell and format-drag it, but I would like a general formula to apply to the whole worksheet.
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-1)
The shortest most compatible version is:
=INDIRECT("RC[-1]",0)
"RC[-1]" means one column to the left. "R[1]C[-1]" is bottom-left.
The second parameter 0 means that the first parameter is interpreted using R1C1 notation.
The other options:
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-1)
Too long in my opinion. But useful if the relative value is dynamic/derived from another cell. e.g.:
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0, A1)
The most simple option:
= RC[-1]
has the disadvantage that you need to turn on R1C1 notation using options, which is a no-go when other people have to use the excel.
When creating your conditional formatting, set the range to which it applies to what you want (the whole sheet), then enter a relative formula (remove the $ signs) as if you were only formatting the upper-left corner.
Excel will properly apply the formatting to the rest of the cells accordingly.
In this example, starting in B1, the left cell would be A1. Just use that--no advanced formula required.
If you're looking for something more advanced, you can play around with column(), row(), and indirect(...).
If you change your cell reference to use R1C1 notation (Tools|Options, General tab), then you can use a simple notation and paste it into any cell.
Now your formula is simply:
=RC[-1]
Instead of writing the very long:
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-1)
You can simply write:
=OFFSET(*Name of your Cell*,0,-1)
Thus for example you can write into Cell B2:
=OFFSET(B2,0,-1)
to reference to cell B1
Still thanks Jason Young!! I would have never come up with this solution without your answer!
fill the A1 cell, with the following formula :
=IF(COLUMN(A1)=1;"";OFFSET(A20;0;-1))&"1"
Then autoextend to right, you get
1| A | B | C | ect ect
2| 1| 11| 111| ect ect
If offset is outside the range of the available cell, you get the #REF! error.
Hope you enjoy.
Even simpler:
=indirect(address(row(), column() - 1))
OFFSET returns a reference relative to the current reference, so if indirect returns the correct reference, you don't need it.
Why not just use:
=ADDRESS(ROW(),COLUMN()-1)
You could use a VBA script that changes the conditional formatting of a selection (you might have to adjust the condition & formatting accordingly):
For Each i In Selection
i.FormatConditions.Delete
i.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=" & i.Offset(0, -1).Address
With i.FormatConditions(1).Font
.Bold = True
End With
Next i
I stumbled upon this thread because I wanted to always reference the "cell to the left" but CRUCIALLY in a non-volatile way (no OFFSET, INDIRECT and similar disasters). Looking the web up and down, no answers. (This thread does not actually provide an answer either.) After some tinkering about I stumbled upon the most astonishing method, which I like to share with this community:
Suppose a starting value of 100 in E6. Suppose I enter a delta to this value in F5, say 5. We would then calculate the continuation value (105) in F6 = E6+F5. If you want to add another step, easy: just copy column F to column G and enter a new delta in G5.
This is what we do, periodically. Each column has a date and these dates MUST BE in chronological order (to help with MATCH etc). Every so often it happens that we forget to enter a step. Now suppose you want to insert a column between F and G (to catch up with your omission) and copy F into the new G (to repopulate the continuation formula). This is NOTHING SHORT of a total disaster. Try it - H6 will now say =F6+H5 and NOT (as we absolutely need it to) =G6+H5. (The new G6 will be correct.)
To make this work, we can obfuscate this banal calculation in the most astonishing manner F6=index($E6:F6;1;columns($E1:F1)-1)+F5. Copy right and you get G6=index($E6:G6;1;columns($E1:G1)-1)+G5.
This should never work, right? Circular reference, clearly! Try it out and be amazed. Excel seems to realize that although the INDEX range spans the cell we are recalculating, that cell itself is not addressed by the INDEX and thus DOES NOT create a circular reference.
So now I am home and dry. Insert a column between F and G and we get exactly what we need: The continuation value in the old H will refer back to the continuation value we inserted in the new G.
Make a named formula "LeftCell"
For those looking for a non-volatile answer, you can accomplish this by using the INDEX function in a named formula.
Select Cell A2
Open Name Manager (Ctrl+F3)
Click New
Name it 'LeftCell' (or whatever you prefer)
For Scope:, select Workbook
In Refers to:, enter the formula:
=INDEX(!A1:!A2, 1)
Click OK and close Name Manager
This tells Excel to always look at the value immediately to the left of the current cell, and will change dynamically as different cells are selected. If the name is used alone it provides the cell's value, but in a range it uses the reference. Credit to this answer about cell references for the idea.
I think this is the easiest answer.
Use a "Name" to reference the offset.
Say you want to sum a column (Column A) all the way to, but not including, the cell holding the summation (say Cell A100); do this:
(I assume you are using A1 referencing when creating the Name; R1C1 can subsequently be switched to)
Click anywhere in the sheet not on the top row - say Cell D9
Define a Named Range called, say "OneCellAbove", but overwrite the 'RefersTo' box with "=D8" (no quotes)
Now, in Cell A100 you can use the formula =SUM(A1:OneCellAbove)
When creating a User Defined Function, I found out that the other answers involving the functions OFFSET and INDIRECT cannot be applied.
Instead, you have to use Application.Caller to refer to the cell the User Defined Function (UDF) has been used in. In a second step, you convert the column's index to the corresponding column's name.
Finally, you are able to reference the left cell using the active worksheet's Range function.
Function my_user_defined_function(argument1, argument2)
' Way to convert a column number to its name copied from StackOverflow
' http://stackoverflow.com/a/10107264
' Answer by Siddarth Rout (http://stackoverflow.com/users/1140579/siddharth-rout)
' License (if applicable due to the small amount of code): CC BY-SA 3.0
colName = Split(Cells(, (Application.Caller(1).Column - 1)).Address, "$")(1)
rowNumber = Application.Caller(1).Row
left_cell_value = ActiveSheet.Range(colName & rowNumber).Value
' Now do something with left_cell_value
Please select the entire sheet and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::
=A1<>XFD1
Format..., select choice of formatting, OK, OK.

Resources