Row or Cell value
How I got the row or column from a cell.
There’s two senarios where the desire would be to get either the row or columbn value of a particular cell. (Does not work across/between Sheets!)
What is the Column Letter OR Row Number of the cell I’m currently IN
In cell F5
is the formula:
=CONCATENATE(
" -> "
,REGEX(REGEX(ADDRESS(ROW(),COLUMN(),4),"[^A-Z0-9]+","","g"),"[0-9]+","",1)
," / "
,REGEX(REGEX(ADDRESS(ROW(),COLUMN(),4),"[^A-Z0-9]+","","g"),"[A-Za-z]+","",1)
," <- "
)
When the formula is evaluated, you get two things:
- A string that has " → " and " ← " on either end and a “/” in the middle. (Done to “decorate” my outputs so I know what I’m getting is what I expect.)
- On either side of the “/” is the Column value “F” and the Row value “5”
These values come from the two “REGEX” lines shown. Both of them share the sequence of:
REGEX( ADDRESS( ROW(), COLUMN(), 4), "[^A-Z0-9]+", "", "g" )
This set of functions asks the system for the ROW()
number internally and the COLUMN()
which is then asked to return as an ADDRESS
(type 4 means relative) F5
vs $F$5
, etc.
The REGEX()
function will strip out anything from the returned address that is not at least one upper case letter “A-Z” or a numeric digit “0-9”.
The outer REGEX
function does the work of stripping off the numeric digits (first case) leaving the letters or the second one which strips off the letters (second case).
What is the value of the CONTENTS of a cell.
In cells A1-A5 are the folowing values (text strings or values of formulas)
|
A |
1 |
B2 |
2 |
$B2 |
3 |
B$2 |
4 |
$B$2 |
5 |
ZZ99 |
Now, in cell “B1” we put this formula:
1|=CONCATENATE(
2| " -> "
3| , REGEX(
4| REGEX(
5| INDIRECT(
6| ADDRESS(
7| ROW(A1)
8| ,COLUMN(A1)
9| ,4
10| ,1
11| )
12| )
13| ,"[^A-Z0-9]+"
14| ,""
15| ,"g"
16| )
17| ,"[0-9]+"
18| ,""
19| ,1
20| )
21| , " / "
22| , REGEX(
23| REGEX(
24| INDIRECT(
25| ADDRESS(
26| ROW(A1)
27| ,COLUMN(A1)
28| ,4
29| ,1
30| )
31| )
32| , "[^A-Z0-9]+"
33| , ""
34| , "g"
35| )
36| , "[A-Za-z]+"
37| , ""
38| , 1
39| )
40| , " <- "
41|)
and then replicate it down column “B” aside of what’s in “A” and you’ll get:
|
A |
B |
1 |
B2 |
→ B / 2 ← |
2 |
$B2 |
→ B / 2 ← |
3 |
B$2 |
→ B / 2 ← |
4 |
$B$2 |
→ B / 2 ← |
5 |
ZZ99 |
→ ZZ / 99 ← |
Both types use the same underlying approach, the difference is the use of the INDIRECT()
function which says "Return the value IN the cell, not just the address of the cell in question.
Hows it work?
The value in the ROW()
and COLUMN()
function calls has to be the same thing in all cases. The function calls of ROW(A1)
returns “1” and COLUMN(A1)
returns “A” which is based on the strings “A1” or whatever value is placed there. (i.e. ROW(D9)
returns “9” and COLUMN(D9)
returns “D”)
So, using the values of D9
instead of A1
to help make it clearer.
The value of:
ADDRESS( ROW(D9), COLUMN(D9), 4, 1 )
returns “D9”. I know that it seems “long winded” to get back to “D9” but the arguments to ROW()
or COLUMN()
can be calculated values so it can be flexable to arrive at specific values.
The return from that function call is handed to the INDIRECT()
function which says “return the value in the cell given in the argument”, which in this case is D9
And that’s the glorious results.
Items to note.
With no argument to ROW()
or COLUMN()
(Used just like that.) returns the value for the cell it is in. Giving arguments to those functions must be the same or else you’re on your own.