在 Excel 中,您可以让一个单元格引用另一个单元格来计算其值。我想做相反的事情,获取引用当前选定单元格的任何单元格。这在 Excel 中是否可行,是作为内置函数之一还是 VBA 脚本?
答案1
无法说如何以编程方式使用它,但是从公式功能区中,有一个“公式审核”窗格,可让您“跟踪依赖项”,并在工作表上从当前选定的单元格到通过公式引用它的任何单元格绘制一个可视箭头。
还有相应的“追踪先例”命令。
答案2
显然,您可以在宏中访问跟踪先例/从属项:宏代码用于追踪从属项/先例@OzGrid.com,并附有链接一篇包含已完成的 VBA 函数的帖子@vbaexpress.com:
Sub FindPrecedents()
' written by Bill Manville
' With edits from PaulS
' this procedure finds the cells which are the direct precedents of the active cell
Dim rLast As Range, iLinkNum As Integer, iArrowNum As Integer
Dim stMsg As String
Dim bNewArrow As Boolean
Application. ScreenUpdating = False
ActiveCell.ShowPrecedents
Set rLast = ActiveCell
iArrowNum = 1
iLinkNum = 1
bNewArrow = True
Do
Do
Application.Goto rLast
On Error Resume Next
ActiveCell.NavigateArrow TowardPrecedent:=True, ArrowNumber:=iArrowNum, LinkNumber:=iLinkNum
If Err.Number > 0 Then Exit Do
On Error Goto 0
If rLast.Address(external:=True) = ActiveCell.Address(external:=True) Then Exit Do
bNewArrow = False
If rLast.Worksheet.Parent.Name = ActiveCell.Worksheet.Parent.Name Then
If rLast.Worksheet.Name = ActiveCell.Parent.Name Then
' local
stMsg = stMsg & vbNewLine & Selection.Address
Else
stMsg = stMsg & vbNewLine & "'" & Selection.Parent.Name & "'!" & Selection.Address
End If
Else
' external
stMsg = stMsg & vbNewLine & Selection.Address(external:=True)
End If
iLinkNum = iLinkNum + 1 ' try another link
Loop
If bNewArrow Then Exit Do
iLinkNum = 1
bNewArrow = True
iArrowNum = iArrowNum + 1 'try another arrow
Loop
rLast.Parent.ClearArrows
Application.Goto rLast
MsgBox "Precedents are" & stMsg
Exit Sub
End Sub
仅供参考,第二篇文章的作者说此功能无法处理对已关闭工作簿的外部引用。这留给读者练习吧。
答案3
答案4
在 Office 2007 中,查看公式并使用跟踪先例或跟踪从属项。我不记得它在早期版本中的位置,但它的操作大致相同