创建宏以跟踪间接函数的先行

时间:2015-08-24 16:20:47

标签: excel vba excel-vba excel-indirect

我正在尝试创建一个跟踪间接公式的先例的宏。间接公式如下:indirect(Sheetname,column,row,TRUE)。我尝试了以下代码。但它不起作用,我不知道为什么。所以,如果有人可以帮助我那将是伟大的。 Thanx提前!

Sub GetCell()
Dim c As Range

On Error Resume Next
Set c = Selection.Parent.Evaluate(Selection.Formula)
On Error GoTo 0
If Not c Is Nothing Then
    If c.Parent.Name <> ActiveSheet.Name Then c.Parent.Activate
    c.Select
End If

End Sub

1 个答案:

答案 0 :(得分:0)

Dim strFormula As String, strBetweenParenthesis As String, strNewFormula as String
Dim lPos As Long, lNextChar As Long, lStart As Long, lEnd As Long
dim rSource as range

Set rSource = ActiveCell
strFormula = rSource.Formula
strNewFormula = strFormula
lPos = InStr(1, strFormula, "INDIRECT")

If lPos > 0 Then

    Do
        lStart = lPos + Len("INDIRECT")
        lEnd = InStr(lStart, strNewFormula, ")")
        strBetweenParenthesis = Mid(strNewFormula, lStart, lEnd - lStart + 1)
        strNewFormula= Replace(strNewFormula, "INDIRECT" & strBetweenParenthesis, Evaluate(strBetweenParenthesis))
        lPos = InStr(lEnd - Len("INDIRECT" & strBetweenParenthesis) + Len(Evaluate(strBetweenParenthesis)), strNewFormula, "INDIRECT")
    Loop Until lPos = 0

    rSource.Formula = strNewFormula
End If

rSource.ShowPrecedents
rSource.NavigateArrow True, 1
rSource.ShowPrecedents(True)
rSource.Formula = strFormula