本文讲述了如何根据单元格内容为参数动态引用Excel其他单元格中的内容,这些单元格可能是同一个sheet,也可以是不同的sheet。
主要使用了两个函数:
Address 用来动态生成单元格地址。
-
Syntax ADDRESS(row_num,column_num,abs_num,a1,sheet_text) Row_num is the row number to use in the cell reference. Column_num is the column number to use in the cell reference. Abs_num specifies the type of reference to return. A1 is a logical value that specifies the A1 or R1C1 reference style. If a1 is TRUE or omitted, ADDRESS returns an A1-style reference; if FALSE, ADDRESS returns an R1C1-style reference. Sheet_text is text specifying the name of the worksheet to be used as the external reference. If sheet_text is omitted, no sheet name is used.
Indirect用来根据指定字符串获得单元格内容。
-
Syntax INDIRECT(ref_text,a1) Ref_text is a reference to a cell that contains an A1-style reference, an R1C1-style reference, a name defined as a reference, or a reference to a cell as a text string. If ref_text is not a valid cell reference, INDIRECT returns the #REF! error value.
- If ref_text refers to another workbook (an external reference), the other workbook must be open. If the source workbook is not open, INDIRECT returns the #REF! error value.
A1 is a logical value that specifies what type of reference is contained in the cell ref_text.
举例:
场景是这样的,有多个项目,每个项目的信息放在一个sheet中,sheet名字就是项目名称,例如:PJ1/PJ2/PJ3。还有一个sheet(Total)汇总所有的信息。
我们希望当增加或删除一个项目时,操作尽可能简单。
可以看到每个项目sheet的A2单元格保存了项目工作量数据。
在total sheet中,目前有三列,从左到右:
- 项目名称,也就是sheet名称
- 单元格地址串:其中sheet名称是根据同一行第一列的内容动态变化的。这一列纯粹为了演示用。
它的内容是 =ADDRESS(1, 2, 1, TRUE,
Ax)其中最后一个参数指定了项目名单元格,每行不同。这样,就得到了一系列的地址字符串PJ1!$B$1 / PJ2!$B$1 / PJ3!$B$1
- 单元格内容:对应项目的工作量。 内容为 =INDIRECT( ADDRESS(1, 2, 1, TRUE, Ax) ),把单元格地址转化为单元格引用。
最后的结果如下:
Project Name |
Cell Address |
Cell Value by cell address |
PJ1 |
PJ1!$B$1 |
100 |
PJ2 |
PJ2!$B$1 |
200 |
PJ3 |
PJ3!$B$1 |
300 |
如果增加一个项目,拷贝一行新的内容,然后,把第一列的名字改成新sheet的名称就可以了。如果想从不同的sheet中获得不同单元格的内容,也可以不address的其他参数改称对单元格的引用