1

在工作中调试或质量检查 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!"是混合单元格中的常量,在本例中为工作表名称。如果该工作表名称发生更改,则公式将中断。我发现并正在使用两种条件格式来突出显示常量单元格和使用此“使用条件格式识别公式”的公式的单元格。我需要想出一种方法来格式化那些在公式中包含这些常量的单元格。

找到了这个问题并尝试使用=IF(COUNT(SEARCH(CHAR(34),A1,1)),TRUE,FALSE)andFIND()看看我是否可以检查一个单元格内部是否有双引号,但是 SEARCH() 会返回FALSE,因为它正在查看单元格的值而不是它的内容。TRUE如果单元格包含则返回,"Constant"但如果它是公式则返回FALSE,例如单元格是否包含="Constant"

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

编辑*

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

Function FormulaHasQuotes(aCell)

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

End Function

FormulaHasQuotes 条件格式图片

4

2 回答 2

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

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

于 2013-09-27T19:23:12.297 回答
0

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

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

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
于 2015-02-05T01:51:22.333 回答