Home > database >  How to encapsulate INDIRECT into INDEX using R/C notation in Excel?
How to encapsulate INDIRECT into INDEX using R/C notation in Excel?

Time:10-08

I created a range using INDIREKT, I added COL() to see how big this range is:

=COL(INDIRECT("R1C1:R1C5"; 0))

As you can see, I'm using the R/C notation for row/colum adressing. The result is simply all numbers from 1 to 5 in the first row:

enter image description here

Now I'm just using the numbers to address letters from a string, like this:

="_" & MID("HELLO"; COL(INDIRECT("Z1S1:Z1S5"; 0)); 1)

This works as expected and returns:

enter image description here

Now there's a random cell somewhere on the sheet, I named this cell "_H". The cell just contains the letter H:

enter image description here

I did this for E, L and O, too.

Now I add a third row containing this.

=INDEX(INDIRECT(A2);1;1)

Pulling this to the width of 5 columns the result is as expected:

enter image description here

And here comes my problem: How to use the R/C notation? This comes into my mind:

=INDIRECT("Z2S1"; 0)

This returns the correct value of row 2, column 1 aka A2. Which is _H. Now I put this into the INDEX formula, expecting that Excel takes the _H, use this as the address for INDEX and returns the value from 1;1 of this "name range":

=INDEX(INDIRECT("R2C1";0);1;1)

But the result is simply:

enter image description here

When I remove the quotes, Excel is complaining, so this is not working:

=INDEX(INDIRECT(R2C1;0);1;1)

(Apparently, because the Excel setting says I prefer A1-notation).

But I wonder why it's not working, when I tell excel explicitly to use the R/C notation?

Well, it seems like Excel just takes the range and the value from R1C1 in this range - which is _R. So I was trying around to make this column-argument "dynamic". I know that this returns the column index for the whole range.

 =COL(INDIRECT("R1C1:R1C5";0))

This returns 1, 2, 3, 4 and 5 for the particular columns. So why not putting it into the formula above:

 =INDEX(INDIRECT("Z2S" & COL(INDIRECT("R1C1:R1C5";0));0);1;COL(INDIRECT("R1C1:R1C5";0)))

This returns #VALUE as an error.

How to indirectly address a cell or even a range to get a value from a particular coordinate (using INDEX)? I specifically need the R/C notation because I need to address ranges numerically (for example column 1 instead of column A and so on).

CodePudding user response:

Ok, it's easier than I thought, Excel has this wonderful MAP formula:

=MAP(A2:E2; LAMBDA(a; INDEX(INDIRECT(a); 1; 1)))

Now every particular column points to the reference/range name and INDEX can use this, to get the value from R1/C1 of the particular named range.

This can even be more improved into a one-liner:

=MAP(SEQUENCE(1;LEN("HELLO")); LAMBDA(i; INDEX(INDIRECT("_" & MID("HELLO"; i; 1));1;1)))

I can put this everywhere I want, I just need to define the named ranges.

Appreciation to @Ike who lead into the right direction.

CodePudding user response:

You can condense your solution - no `INDEX(X;1;1) needed:

=LET(word,"HELLO",
     singleCharacters,"_" & MID(word,SEQUENCE(1,LEN(word)),1),
     MAP(singleCharacters,LAMBDA(a,INDIRECT(a))))

Using LET makes it a bit more readable.

  • Related