如何确定单元格公式是否包含常量?

当在工作中debugging或质量检查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)FIND()来查看是否可以检查单元格是否有双引号内,但是SEARCH()返回FALSE因为它正在查看单元格值而不是内容。 如果单元格包含"Constant" ,则返回TRUE如果是公式,则返回FALSE ,例如单元格包含="Constant"

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

编辑*

感谢Sidd的代码,我已经在模块中使用了条件格式,至less可以突出显示单元格中包含引号的单元格。

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

FormulaHasQuotes条件格式图片

假设你的工作表是这样的。

在这里输入图像说明

这是你正在尝试?

 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 

注意:我已经向您展示了如何为一个工作表执行此操作,我相信您可以将其复制到工作簿中的所有工作表中。

只有非常轻微的testing…

例如不会处理“string”常量,其中包含embedded的"

分testing仪()

 '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 = "[^AZ$](-?\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