【问题标题】:How to determine if a cell formula contains Constants?如何确定单元格公式是否包含常量?
【发布时间】:2013-10-04 04:37:42
【问题描述】:

在工作中调试或检查 Excel 报告时,我发现问题是由于文本在公式中被硬编码。我听说这是一个常量和公式混合单元格。

以下是我所看到的示例。

常量 =100

常量 =Facility

公式单元格=INDIRECT(ADDRESS(5,MATCH($A7&$B7&C$2,Data!$4:$4,0),,,$A$2))

混合单元格=INDIRECT("Data!"&ADDRESS(5,MATCH($A7&$B7&C$2,Data!$4:$4,0)))

"Data!" 是混合单元格中的常量,在本例中为工作表名称。如果该工作表名称发生更改,则公式将中断。我发现并正在使用两种条件格式来突出显示常量单元格和使用 "Identify formulas using Conditional Formatting" 的公式单元格。我需要想出一种方法来格式化那些在公式中包含这些常量的单元格。

我有 found this question 并尝试使用 =IF(COUNT(SEARCH(CHAR(34),A1,1)),TRUE,FALSE)FIND() 来查看是否可以检查单元格中是否包含双引号,但 SEARCH() 会返回 FALSE,因为它正在查看单元格价值而不是它的内容。如果单元格包含"Constant",则返回TRUE,但如果是公式,则返回FALSE,例如单元格包含="Constant"

如何在整个工作表或工作簿的公式中找到常量?

编辑*

感谢下面 Sidd 的代码,我在一个模块中创建了一个函数,我可以在条件格式中使用它来至少突出显示单元格内包含引号的单元格。

Function FormulaHasQuotes(aCell)

  If InStr(1, aCell.Formula, """") Then
      FormulaHasQuotes = True
  Else
      FormulaHasQuotes = False
  End If

End Function

【问题讨论】:

  • 其复杂且并非万无一失,您需要解析公式。我的插件Mappit! 使用正则表达式来识别公式中的常量,但在上面的情况下,它会选择5 而不是Data!,这实际上是一个位置而不是一个常量。
  • 非常令人印象深刻的插件谢谢。

标签: excel excel-2007 excel-formula vba


【解决方案1】:

假设您的工作表如下所示。

这是你正在尝试的吗?

Sub Sample()
    Dim ws As Worksheet
    Dim aCell As Range, FRange As Range

    '~~> Set this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find all the cells which have formula
        On Error Resume Next
        Set FRange = .Cells.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0

        If Not FRange Is Nothing Then
            '~~> Check for " in the cell's formula
            For Each aCell In FRange
                If InStr(1, aCell.Formula, """") Then
                    Debug.Print "Cell " & aCell.Address; " has a constant"
                End If
            Next
        End If
    End With
End Sub

当你运行上面的代码时,你会得到这个输出

Cell $A$2 has a constant
Cell $A$5 has a constant

注意:我已经向您展示了如何为工作表执行此操作,我确定您可以为工作簿中的所有工作表复制它?

【讨论】:

  • 那很好。但是 3 和 4 不是也包含常量吗?
  • @Steve:根据 OP,QUOTES 中的任何内容都是常量。现在Constant 这里不会与124 等实际常量混淆...至少这是我从问题中了解到的...
  • 第一个例子是 Constant =100
  • @Steve:是的。但我没有看到它被用于 OP 帖子的公式中。然而我看到的是if I could check if a cell had double quotes inside of it...让我们等待OP澄清:)
  • 谢谢 Sidd,这将在大多数情况下(如果不是所有情况)对我有所帮助。但是史蒂夫确实有一点,地址公式包含常量 5 我不确定是否有办法显示单元格是否包含像 5 这样的硬编码数字。
【解决方案2】:

只有非常轻微的测试......

例如将不处理包含嵌入" 的“字符串”常量

Sub Tester()

'add reference to "Microsoft VBScript Regular Expressions 5.5"
Dim regxnum As New RegExp, regexpchar As New RegExp
Dim matches As MatchCollection, m As Match

Dim rngF As Range, c As Range

    On Error Resume Next
    Set rngF = ActiveSheet.UsedRange.SpecialCells(xlFormulas)
    On Error GoTo 0

    If Not rngF Is Nothing Then

        regxnum.Pattern = "[^A-Z$](-?\d{1,}\.?\d{0,})"
        regxnum.Global = True

        regexpchar.Pattern = "(""[^""]{0,}"")"
        regexpchar.Global = True

        For Each c In rngF.Cells
            Debug.Print c.Formula
            Set matches = regxnum.Execute(c.Formula)

            For Each m In matches
                    Debug.Print c.Parent.Name, c.Address(), m.SubMatches(0)
            Next m

            Set matches = regexpchar.Execute(c.Formula)
            For Each m In matches
                    Debug.Print c.Parent.Name, c.Address(), m.SubMatches(0)
            Next m

        Next c
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多