我有一个 Excel 电子表格,里面有很多我跟踪的公式和数据。我有一个小宏,可以找到选定单元格的先例,但是我希望使宏递归,以便我可以找到所有先例。例如,将焦点设置到一个单元格并运行此函数将突出显示该单元格,然后突出显示该单元格的先例,然后突出显示这些单元格的先例,然后突出显示该先例......
我目前遇到的问题是我不确定逃生条件应该是什么。我遇到了一些无限循环问题,并且对递归不够熟悉,无法找到可靠的解决方案。
下面是我用来(正确)找到初始先例的一些代码:
Sub FindClosedWbReferences(inRange As Range)
Rem fills the collection With closed precedents parsed from the formula String
Dim testString As String, returnStr As String, remnantStr As String
testString = inRange.Formula
testString = RemoveTextInDoubleQuotes(testString): Rem New line
Set ClosedWbRefs = New Collection
Do
returnStr = NextClosedWbRefStr(testString, remnantStr)
ClosedWbRefs.Add Item:=returnStr, Key:=CStr(ClosedWbRefs.count)
testString = remnantStr
inRange.Select
inRange.Interior.ColorIndex = 36
Loop Until returnStr = vbNullString
ClosedWbRefs.Remove ClosedWbRefs.count
End Sub
这是从一个类似于以下内容的主函数调用的:
If homeCell.HasFormula Then
Set OtherWbRefs = New Collection: CountOfClosedWb = 0
Set SameWbOtherSheetRefs = New Collection
Set SameWbSameSheetRefs = New Collection
Rem find closed precedents from formula String
Call FindClosedWbReferences(homeCell)
任何帮助表示赞赏。谢谢