这是RegEx用于匹配Excel公式中的任何单元格引用吗?

Kuy*_*nda 5 regex excel vba excel-formula

我一直在尝试创建一个正则表达式模式,匹配任何Excel公式中的任何引用,包括绝对,相对和外部引用.我需要返回整个引用,包括工作表和工作簿名称.

我无法找到有关Excel A1表示法的详尽文档,但经过大量测试后我确定了以下内容:

  • 公式前面带有等号"="
  • 公式中的字符串用双引号括起来,在查找实际引​​用之前需要删除,否则=A1&"A1"会破坏正则表达式
  • 工作表名称最长可达31个字符,不包括\ /?*[]:
  • 外部引用中的工作表名称必须以bang成功 =Sheet1!A1
  • 外部引用中的工作簿名称必须括在方括号中 =[Book1.xlsx]Sheet1!A1
  • 如果引用是封闭工作簿中的范围,则Excel添加的工作簿路径始终用单引号括起来,并且括在工作簿名称括号的左侧 'C:\[Book1.xlsx]Sheet1'!A1
  • 某些字符(例如,不间断的空格)导致Excel将工作簿和工作表名称用单引号括在外部引用中,但我不知道具体哪些字符 ='[Book 1.xlsx]Sheet 1'!A1
  • 即使启用了R1C1表示法,Range.Formula仍会以A1表示法返回引用.Range.FormulaR1C1返回R1C1表示法中的引用.
  • 3D参考样式允许在一个工作簿上使用一系列工作表名称 =SUM([Book5]Sheet1:Sheet3!A1)
  • 命名范围可以在公式中指定:
    • 名称的第一个字符必须是字母,下划线字符(_)或反斜杠(\).名称中的剩余字符可以是字母,数字,句点和下划线字符.
    • 您不能将大写和小写字符"C","c","R"或"r"用作已定义的名称,因为它们都用作选择当前所选单元格的行或列的简写.在名称或转到文本框中输入它们.
    • 名称不能与单元格引用相同,例如Z $ 100或R1C1.
    • 空格不允许作为名称的一部分.
    • 名称最长可达255个字符.
    • 名称可以包含大写和小写字母.Excel不区分名称中的大写和小写字符.

以下是我提出的用于测试的VBA程序.我更新了代码以处理名称:

Sub ReturnFormulaReferences()

    Dim objRegExp As New VBScript_RegExp_55.RegExp
    Dim objCell As Range
    Dim objStringMatches As Object
    Dim objReferenceMatches As Object
    Dim objMatch As Object
    Dim intReferenceCount As Integer
    Dim intIndex As Integer
    Dim booIsReference As Boolean
    Dim objName As Name
    Dim booNameFound As Boolean

    With objRegExp
        .MultiLine = True
        .Global = True
        .IgnoreCase = True
    End With

    For Each objCell In Selection.Cells
        If Left(objCell.Formula, 1) = "=" Then

            objRegExp.Pattern = "\"".*\"""
            Set objStringMatches = objRegExp.Execute(objCell.Formula)

            objRegExp.Pattern = "(\'.*(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\'\!" _
            & "|(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\!)?" _
            & "(\$?[a-z]{1,3}\$?[0-9]{1,7}(\:\$?[a-z]{1,3}\$?[0-9]{1,7})?" _
            & "|\$[a-z]{1,3}\:\$[a-z]{1,3}" _
            & "|[a-z]{1,3}\:[a-z]{1,3}" _
            & "|\$[0-9]{1,7}\:\$[0-9]{1,7}" _
            & "|[0-9]{1,7}\:[0-9]{1,7}" _
            & "|[a-z_\\][a-z0-9_\.]{0,254})"
            Set objReferenceMatches = objRegExp.Execute(objCell.Formula)

            intReferenceCount = 0
            For Each objMatch In objReferenceMatches
                intReferenceCount = intReferenceCount + 1
            Next

            Debug.Print objCell.Formula
            For intIndex = intReferenceCount - 1 To 0 Step -1
                booIsReference = True
                For Each objMatch In objStringMatches
                    If objReferenceMatches(intIndex).FirstIndex > objMatch.FirstIndex _
                    And objReferenceMatches(intIndex).FirstIndex < objMatch.FirstIndex + objMatch.Length Then
                        booIsReference = False
                        Exit For
                    End If
                Next

                If booIsReference Then
                    objRegExp.Pattern = "(\'.*(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\'\!" _
                    & "|(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\!)?" _
                    & "(\$?[a-z]{1,3}\$?[0-9]{1,7}(\:\$?[a-z]{1,3}\$?[0-9]{1,7})?" _
                    & "|\$[a-z]{1,3}\:\$[a-z]{1,3}" _
                    & "|[a-z]{1,3}\:[a-z]{1,3}" _
                    & "|\$[0-9]{1,7}\:\$[0-9]{1,7}" _
                    & "|[0-9]{1,7}\:[0-9]{1,7})"
                    If Not objRegExp.Test(objReferenceMatches(intIndex).Value) Then 'reference is not A1
                        objRegExp.Pattern = "^(\'.*(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\'\!" _
                        & "|(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\!)" _
                        & "[a-z_\\][a-z0-9_\.]{0,254}$"
                        If Not objRegExp.Test(objReferenceMatches(intIndex).Value) Then 'name is not external
                            booNameFound = False
                            For Each objName In objCell.Worksheet.Parent.Names
                                If objReferenceMatches(intIndex).Value = objName.Name Then
                                    booNameFound = True
                                    Exit For
                                End If
                            Next
                            If Not booNameFound Then
                                objRegExp.Pattern = "^(\'.*(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\'\!" _
                                & "|(\[.*\])?([^\:\\\/\?\*\[\]]{1,31}\:)?[^\:\\\/\?\*\[\]]{1,31}\!)"
                                For Each objName In objCell.Worksheet.Names
                                    If objReferenceMatches(intIndex).Value = objRegExp.Replace(objName.Name, "") Then
                                        booNameFound = True
                                        Exit For
                                    End If
                                Next
                            End If
                            booIsReference = booNameFound
                        End If
                    End If
                End If

                If booIsReference Then
                    Debug.Print "  " & objReferenceMatches(intIndex).Value _
                    & " (" & objReferenceMatches(intIndex).FirstIndex & ", " _
                    & objReferenceMatches(intIndex).Length & ")"
                End If
            Next intIndex
            Debug.Print

        End If
    Next

    Set objRegExp = Nothing
    Set objStringMatches = Nothing
    Set objReferenceMatches = Nothing
    Set objMatch = Nothing
    Set objCell = Nothing
    Set objName = Nothing

End Sub
Run Code Online (Sandbox Code Playgroud)

谁能打破或改善这个?如果没有关于Excel公式语法的详尽文档,很难知道这是否正确.

谢谢!

Kuy*_*nda 3

jtolle 引导我走向正确的方向。据我所知,这就是我试图做的事情。我一直在测试,它似乎有效。

stringOriginFormula = rangeOrigin.Formula
rangeOrigin.Cut rangeDestination
rangeOrigin.Formula = stringOriginFormula
Run Code Online (Sandbox Code Playgroud)

谢谢杰托勒!