Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google spreadsheet formula to resolve string as a cell

Using google spreadsheets is there a way to evaluate a cell value as the row in a lookup operation? For example rather than =D2 to grab the value of D2, I want to evaluate the value of a cell and use it as the row to lookup another cell. I've looked at the google spreadsheet formula documentation and haven't been able to find a solution.

The below pseudocode illustrates what I'm trying to do.

        A     B    C    D
    1         D
    2       =[B1]2     10
    3       =[B1]3      9
    4       =[B1]4      8

Given the value of B1 is "D" I want cells B2, B3, and B4 to resolve to 10, 9, and 8 respectively.

like image 687
Steven Avatar asked Jan 19 '13 22:01

Steven


People also ask

How do I convert a string to a formula in Google Sheets?

When you need to construct a formula instead of directly type a formula, use the INDIRECT function. It turns text strings into a cell reference. For example, if you need to have a formula that references a column of data. You may want the same formula for a different column.

How do I extract text from a string in Google Sheets?

To extract text from the middle of a text string, we can use the RIGHT, SEARCH and LEN functions to get the text from the right side of the string, and then use the MID and LEN functions to get the text in the middle. We will also incorporate the TRIM function to trim any spaces on either side of the text string.

How do you write an IF THEN formula in Google Sheets?

Using the IF Function The IF function can be used on its own in a single logical test, or you can nest multiple IF statements into a single formula for more complex tests. To start, open your Google Sheets spreadsheet and then type =IF(test, value_if_true, value_if_false) into a cell.


2 Answers

You might be looking for something like this:

=INDIRECT(INDEX(B$1;1;1)&ROW())

  1. the INDEX(B$1;1;1) gets content of B1 cell (the 1;1 is obligatory parameter, since you can feed INDEX with range and means: 1st row in range;1st column in range which is D
  2. ROW() returns current row number, be it 2, 3 etc.
  3. & concatenates both value, so the result is processed like "D" & "2" => "D2"
  4. INDIRECT(reference) returns reference to a cell or an area (in text form) for which to return the contents.

see https://support.google.com/drive/table/25273?hl=en

Still, depending on what is the original problem, there might be better solutions. Above one is just straightforward answer to your question.

like image 198
PsychoFish Avatar answered Sep 18 '22 17:09

PsychoFish


Bearing in mind the correctness and helpfulness of PsychoFish's advice on the use of INDEX() with INDIRECT(), and on the limited usefulness of any one solution for all problems, I think the following formula will work as well in this particular case:

=INDIRECT(B$1&Row())
like image 23
SquarePowder Avatar answered Sep 21 '22 17:09

SquarePowder