简体   繁体   English

Excel:获得一个公式中引用的地址,并在另一个公式中使用它

[英]Excel: get the address referenced in one formula, and use it in another formula

Is there a way to obtain the address of a referenced cell, in Excel, in a way that it can be used in another formula? 有没有一种方法可以在Excel中以一种可以在另一个公式中使用的方式来获取引用单元格的地址?

I have a cell that references another, contents of B1 =Address!A4. 我有一个单元格引用另一个B1 = Address!A4的内容。 In another cell I want to get the cell reference contained in B1 and use it to obtain the value in a cell relative to the referenced cell. 在另一个单元格中,我想获取B1中包含的单元格引用,并使用它来获取相对于引用单元格的单元格中的值。 I do not want to search for the value of the cell Address!A4 because the range in which I would be searching may contain multiple matches. 我不想搜索单元格Address!A4的值,因为我要搜索的范围可能包含多个匹配项。 I want a reference to the cell. 我想要引用该单元格。

Is there a way to do this? 有没有办法做到这一点?

For example: 例如:

a row of cells contains values A1 ="123 E 400 S", B1 ="123", C1 ="400" 一行单元格包含值A1 =“ 123 E 400 S”, B1 =“ 123”, C1 =“ 400”
a cell contains a reference to one of those cells: G8 = B1 一个单元格包含对这些单元格之一的引用: G8 = B1

So in the cell G8 the value "123" is displayed. 因此,在单元格G8中,将显示值“ 123”。 I want to place in cell F8 something that will use the reference in G8 as a "pointer" or index to B1 that I can then offset to get at A1 . 我想在单元格F8中放置一些东西,它将G8中的引用用作B1的“指针”或索引,然后我可以将其偏移以获得A1

I would like to do this F8 =OFFSET(CELL("contents", G8 ),0,-1) which would cause the value "123 E 400 S" to be displayed. 我想执行此操作F8 = OFFSET(CELL(“ contents”, G8 ),0,-1),这将导致显示值“ 123 E 400 S”。 Sadly, this does not work, I get a #REF! 可悲的是,这行不通,我得到一个#REF! error. 错误。

This may seem like a candidate for INDEX, VLOOKUP, MATCH, etc., but I do not want to try to define the range of cells to look in, it may change. 这看起来像是INDEX,VLOOKUP,MATCH等的候选对象,但是我不想尝试定义要查看的单元格范围,它可能会改变。 And the potential search string "123" may be duplicated in the range even if I were to define it. 并且即使我要定义它,潜在的搜索字符串“ 123”也可能在该范围内重复。

It seems like I should be able to use the reference I already know, go to that cell, and then work with cells relative to that. 看来我应该能够使用我已经知道的引用,转到该单元格,然后使用相对于此的单元格。

Any ideas? 有任何想法吗? Thanks for the help. 谢谢您的帮助。

Edit: An easier method 编辑: 更简单的方法

It might be easier to just have the cell address in a cell somewhere, and use INDIRECT from both cells B1 and C1, then you don't need the extra functions described below! 这可能是更容易只是在某个单元格中的单元格地址,并使用INDIRECT两个单元B1和C1,那么你就需要下面描述的附加功能!

间接


VBA method: User-defined functions VBA方法:用户定义的功能

If you can save your workbook as a macro-enabled .xlsm then you could use a User-defined function to get the formula, and extract the cell address. 如果您可以将工作簿另存为启用了宏的.xlsm则可以使用用户定义的函数来获取公式,并提取单元格地址。 Add this to a module in your workbook's VBA editor (press Alt + F11 , Insert>Module). 将此添加到工作簿的VBA编辑器中的模块中(按Alt + F11 ,依次单击“插入”>“模块”)。

Function GetFormula(rng As Range) As String
    GetFormula = rng.Formula
End Function

Example use, note the formula in C1 is =GetFormula , but the value shown is the formula in B1: 使用示例,请注意C1中的公式为=GetFormula ,但显示的值为B1中的公式:

获得功能

Then you can either use the cell formula RIGHT , or do it directly in VBA with another function. 然后,您可以使用单元格公式RIGHT ,也可以直接在VBA中使用另一个函数进行操作。 The advantage of doing it in VBA is any parsing you do will be clearer, you could go as far as using RegExp. 在VBA中执行此操作的优点是,您所做的任何解析都将更加清晰,您可以使用RegExp。

Function GetEndRange(rng As Range) As String
    Dim str As String
    str = rng.Formula
    ' Get the characters to the right of an exclamation point
    ' Assumes formula ended in an address, could do more complicated parsing here
    str = Right(str, Len(str) - InStr(str, "!"))
    GetEndRange = str
End Function

Use: 采用:

终点范围

Now you can use a cell formula (or a more complicated macro) to access the offset cell. 现在,您可以使用单元格公式(或更复杂的宏)来访问偏移单元格。 Here is the cell formula way to do it: 这是执行单元格公式的方法:

=OFFSET(INDIRECT("Address!" & GetEndRange(B1),TRUE),1,0)
' Evaluated as:
' =OFFSET(INDIRECT("Address!A4",TRUE),1,0)
' =OFFSET(Address!$A$4,1,0)
' =Address!$A$5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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