[英]find cell reference of a cell from another cell in excel
I have a worksheet that returns a value from a different sheet in the sameworkbook (=data!C68) which returns a value. 我有一个工作表,它从同一工作簿(= data!C68)中的另一个工作表返回一个值,该工作表返回一个值。
Ina another cell I want to reference the reference in the original cell but increment the row by 1 - (=data!C69). 在另一个单元格中,我想引用原始单元格中的引用,但将行增加1-(= data!C69)。 The cells are not next to each other. 单元不彼此相邻。
Is this possible either with a function or a fromula? 通过功能或小舌可以做到吗?
Thanks 谢谢
Jonathan 乔纳森
There are many ways to achieve this. 有很多方法可以实现这一目标。 Here is one way. 这是一种方法。 I have extended the function by incorporating the rows and columns that you want to Offset. 我通过合并要偏移的行和列来扩展了功能。 Paste this code in a module 将此代码粘贴到模块中
Syntax 句法
=GetOffset(Range,Row,Col) = GetOffset(范围,行,颜色)
Remarks 备注
Range (Required) is the cell which has the formula 范围 (必填)是具有公式的单元格
Row (Optional) Row(s) you want to offset 行 (可选)要偏移的行
Col (Optional) Column(s) you want to offset 上校 (可选)列(S)要偏移
Sample Usage 样品用量
=GetOffset(A1) : This will pickup the value from the same cell which A1 is referring to. = GetOffset(A1):这将从A1所引用的同一单元格中获取值。 If A1 is referring to =data!C69
then GetOffset will get value from data!C69 如果A1引用=data!C69
则GetOffset将从data!C69获取值。
=GetOffset(A1,1) : This will pickup the value from the next cell (1 row down) which A1 is referring to. = GetOffset(A1,1):这将从A1所引用的下一个单元格(向下1行)中提取值。 If A1 is referring to =data!C69
then GetOffset will get value from data!C70 如果A1引用=data!C69
则GetOffset将从data!C70获取值。
=GetOffset(A1,1,1) : This will pickup the value from the next cell (1 row down across 1 Col) which A1 is referring to. = GetOffset(A1,1,1):这将从A1所引用的下一个单元格(1行向下1行)中提取值。 If A1 is referring to =data!C69
then GetOffset will get value from data!D70 如果A1引用=data!C69
则GetOffset将从data!D70获取值。
=GetOffset(A1,0,1) : This will pickup the value from the next cell (Same row across 1 Col) which A1 is referring to. = GetOffset(A1,0,1):这将从A1所引用的下一个单元格(跨过1 Col的同一行)中获取值。 If A1 is referring to =data!C69
then GetOffset will get value from data!D69 如果A1引用=data!C69
则GetOffset将从data!D69获取值。
CODE 码
Public Function GetOffset(Rng As Range, Rw As Long, Col As Long) As Variant
Dim MyArray() As String, strTemp As String
Dim TempRow As Long, TempCol As Long
On Error GoTo Whoa
Application.Volatile
MyArray = Split(Rng.Formula, "!")
TempRow = Range(MyArray(UBound(MyArray))).Row
TempCol = Range(MyArray(UBound(MyArray))).Column
TempRow = TempRow + Rw
TempCol = TempCol + Col
strTemp = MyArray(0) & "!" & Cells(TempRow, TempCol).Address
GetOffset = Application.Evaluate(Trim(strTemp))
Exit Function
Whoa:
End Function
Option Explicit
Function GetCellReferenceUsedInFormula(ByVal src As Range) As Range
If src.HasFormula Then
Set GetCellReferenceUsedInFormula = Evaluate(Mid(src.Formula, 2))
End If
End Function
And use the above function in formula with OFFSET
并在带有OFFSET
公式中使用以上函数
=OFFSET(GetCellReferenceUsedInFormula(A1),1,0)
This is assuming that cell A1 contains the formula =data!C68
. 假设单元格A1包含公式=data!C68
。
Please note that the VBA code assumes that the formula is simple (ie =somecellreference
). 请注意,VBA代码假定公式很简单(即=somecellreference
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.