Find all used references in Excel formula

前端 未结 3 763
春和景丽
春和景丽 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 18:01

    Just to provide you an alternative... NOTE THAT THIS will return duplicate result if the cells are called more than once

    Sub testing()
    Dim result As Object
    Dim r As Range
    Dim testExpression As String
    Dim objRegEx As Object
    
    Set r = Cells(1, 2)  ' INPUT THE CELL HERE , e.g.    cells("A1")
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.IgnoreCase = True
    objRegEx.Global = True
    objRegEx.Pattern = """.*"""  ' remove expressions
    testExpression = CStr(r.Formula)
    testExpression = objRegEx.Replace(testExpression, "")
    objRegEx.Pattern = "(([A-Z])+(\d)+)"  'grab the address
    
    If objRegEx.test(testExpression) Then
        Set result = objRegEx.Execute(testExpression)
        If result.Count > 0 Then
            For Each Match In result
                Debug.Print Match.Value
            Next Match
        End If
    End If
    End Sub
    

    Results are stored in "Match.Value"

提交回复
热议问题