简体   繁体   中英

How do I dynamically change input cell in a formula based on the value in another cell in a spreadsheet?

It is possible to dynamically change the input cell address in a formula based on the content in another cell?

Let's say I have a spreadsheet (excel or libreoffice) with these cell values:

A1: 10
A5:  9
C1:  5

Instead of hardcoding =A1-A5 , I would like to do something like this: =A1-A(C1) , that would be evaluated at run time to use cell A5 for the second input.

The non-volatile¹ solution would be the INDEX function .

'for Excel
=A1-INDEX(A:A, C1)
'for OpenOffice
=A1-INDEX(A$1:A$1048576; C1)

nonvolatile_index
nonvolatile_index_oo


¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. Examples of volatile functions are INDIRECT , OFFSET , TODAY , NOW , RAND and RANDBETWEEN . Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well.

Use INDIRECT to take the value in C1 as a pointer to the row that you want in column A:-

=A1-INDIRECT("A"&C1)

(tested in Open Office and Excel 2010)

For this kind of dynamic reference, you need the INDIRECT function. It takes two arguments INDIRECT(reference,style)

reference , a text string containing a cell, a range of cells text or a named range

and style a boolean that if omitted or TRUE, indicates that reference is A1 style, and when FALSE, the reference is using the R1C1 style.

so in your case you can use

INDIRECT("A"&C1)

or

INDIRECT("R1C"&C1,FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM