Excel 如何获取单元格公式引用的工作表 函数链接表(rgCell作为范围)作为工作表 '返回单元格公式引用的工作表 '如果没有公式,则不返回任何内容 作为字符串的Dim strFormula strFormula=rgCell.Cells(1,1).公式 如果(strFormula“”),则 '返回此范围链接到的工作表 如果结束 端函数
有人能帮我完成这个功能吗?请记住,这适用于内部链接、外部链接以及指向名称中有空格(如“Sheet 1”)的图纸的链接 编辑: 为了回应悉达思·劳特,我以前试过Excel 如何获取单元格公式引用的工作表 函数链接表(rgCell作为范围)作为工作表 '返回单元格公式引用的工作表 '如果没有公式,则不返回任何内容 作为字符串的Dim strFormula strFormula=rgCell.Cells(1,1).公式 如果(strFormula“”),则 '返回此范围链接到的工作表 如果结束 端函数,excel,vba,Excel,Vba,有人能帮我完成这个功能吗?请记住,这适用于内部链接、外部链接以及指向名称中有空格(如“Sheet 1”)的图纸的链接 编辑: 为了回应悉达思·劳特,我以前试过 Function LinkedSheet(rgCell As Range) As Worksheet 'Returns the worksheet that the cell formula references 'Returns nothing if there's no formula Dim strFormula As St
Function LinkedSheet(rgCell As Range) As Worksheet
'Returns the worksheet that the cell formula references
'Returns nothing if there's no formula
Dim strFormula As String
strFormula = rgCell.Cells(1, 1).Formula
If (strFormula <> "") Then
'Return the sheet that this range is linked to
End If
End Function
函数链接表(rgCell作为范围)作为工作表
'返回单元格公式引用的工作表
'如果没有公式,则不返回任何内容
Dim strFormula为字符串,sheetName为字符串
strFormula=rgCell.Cells(1,1).公式
如果(strFormula“”),则
'返回此范围链接到的工作表
sheetName=Mid(strFormula,2,InStr(1,strFormula,“!”)-2)
Set LinkedSheet=此工作簿。工作表(sheetName)
如果结束
端函数
对于名称中带有空格的工作表,此操作失败。然而,我不愿意发表这篇文章,因为我觉得必须有一种更好、更有效的方法来解决这个问题,我不想把人们的想法引向与我相同的方向。这是我的解决方案
Function LinkedSheet(rgCell As Range) As Worksheet
'Returns the worksheet that the cell formula references
'Returns nothing if there's no formula
Dim strFormula As String, sheetName As String
strFormula = rgCell.Cells(1, 1).Formula
If (strFormula <> "") Then
'Return the sheet that this range is linked to
sheetName = Mid(strFormula, 2, InStr(1, strFormula, "!") - 2)
Set LinkedSheet = ThisWorkbook.Worksheets(sheetName)
End If
End Function
函数链接表(rgCell作为范围)作为工作表
'返回单元格公式引用的工作表
'如果没有公式,则不返回任何内容
Dim strFormula为字符串,sheetName为字符串
strFormula=rgCell.Cells(1,1).公式
如果(strFormula“”),则
'返回此范围链接到的工作表
如果(InStr(1,strFormula,“=”)=0),则
sheetName=Mid(strFormula,2,InStr(1,strFormula,“!”)-2)
其他的
sheetName=Mid(标准公式,3,仪表(1,标准公式,“!”)-4)
如果结束
Set LinkedSheet=此工作簿。工作表(sheetName)
如果结束
端函数
我并不完全满意。我仍然认为可能有更好的方法,但这是有效的。请告诉我们您尝试了什么?询问代码的问题必须表明对正在解决的问题的最低理解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。另请参见:正如一个稍纵即逝的想法:找到
的位置公式字符串中的代码>如果引用的图纸名称中有空格,则sheetName的值将实际名称用单个记号括起来。您需要删除这些。@如果工作表名称有一个类似“andy’s sheet”的撇号,andy不会失败吗?您是对的。我删除了我的评论。FWIW,Excel会自动转义工作表名称中的一个撇号(带有另一个撇号),因此简单地替换它们是行不通的。
Function LinkedSheet(rgCell As Range) As Worksheet
'Returns the worksheet that the cell formula references
'Returns nothing if there's no formula
Dim strFormula As String, sheetName As String
strFormula = rgCell.Cells(1, 1).Formula
If (strFormula <> "") Then
'Return the sheet that this range is linked to
If (InStr(1, strFormula, "='") = 0) Then
sheetName = Mid(strFormula, 2, InStr(1, strFormula, "!") - 2)
Else
sheetName = Mid(strFormula, 3, InStr(1, strFormula, "!") - 4)
End If
Set LinkedSheet = ThisWorkbook.Worksheets(sheetName)
End If
End Function