[英]Worksheet Name in Cell / How do I write the formula so I can Call a Value in the Worksheet that's referenced in cell
[英]How do I get the worksheet referenced by a cell formula
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
有人可以帮我完成此功能吗? 请记住,这应该适用于内部链接,外部链接以及指向名称为“ 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 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
对于名称中带有空格的工作表,此操作将失败。 但是,我不愿意发布此消息,因为我认为必须有一种更好,更有效的方法来解决问题,并且我不想朝着同一方向传达人们的想法。
这是我的解决方案
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
我对此并不完全满意。 我仍然认为可能会有更好的方法,但这是可行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.