Find all used references in Excel formula

前端 未结 3 766
春和景丽
春和景丽 2020-12-10 17:07

Below is the example set in Excel,

[column1] [column2]

A1  =C3-C5

A2  =((C4-C6)/C6)

A3  =C4*C3

A4  =C6/C7

A5  =C6*C4*C3

I need to extr

3条回答
  •  被撕碎了的回忆
    2020-12-10 17:40

    This function returns you a comma separated list of source cells (precedents):

    Function References(rngSource As Range) As Variant
        Dim rngRef As Range
        Dim strTemp As String
        On Error Resume Next
        For Each rngRef In rngSource.Precedents.Cells
            strTemp = strTemp & ", " & rngRef.Address(False, False)
        Next
        If Len(strTemp)  0 Then strTemp = Mid(strTemp, 3)
        References = strTemp
    End Function
    

    However, please note that you cannot use this as a UDF in the worksheet, as rngRef.Address unfortunately causes a circular reference. However, you can use it in a small procedure to populate another column, e.g.

    Sub ShowPrecedents()
        Dim rng As Range
        'Will paste precedents of A1:A6 into D1:D6
        For Each rng In Range("D1:D6")
            rng.Value = References(rng.Offset(, -3))
        Next
    End Sub
    

提交回复
热议问题