突出显示“硬编码”范围内的单元格,而不是公式派生的单元格

在build立模型后,我在Stackoverflow VBA上find了这个代码(在Excel中突出显示Hardcode单元格(ie1234))

Public Sub hightlightNoFormulas() Dim yourRange as Range, rangeNoFormula as Range Set yourRange = Range("A1:A100") Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas Then loop through your range, excluding any values that have formulas Dim rng as Range For Each rng in yourRange If Intersect(rng,rangeNoFormula) Is Nothing Then rng.interior.Color = 65535 End If Next rng Exit Sub 

虽然在Excel 2010中"Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas"部分错误。我一直在寻找代码,突出显示在“硬编码”,而不是公式派生的可选范围的单元格(即单元格公式是over-键入)。 有人可以提供帮助吗? 谢谢。

你不需要循环…………..只是颜色常量:

 Public Sub hightlightNoFormulas() Dim yourRange As Range, rangeNoFormula As Range Set yourRange = Range("A1:A100") Set rangeNoFormula = yourRange.Cells.SpecialCells(xlCellTypeConstants) rangeNoFormula.Interior.Color = 65535 End Sub 

我结束了修改这个代码,并添加一个input框来select我想要检查的范围。 代码很适合我的需求。 感谢大家!

  Public Sub hightlightNoFormulas() Dim yourRange As Range, rangeNoFormula As Range On Error GoTo GetOut 'if no hard coded cells are found in the range, go to "GetOut" to exit Sub 'Set Range as popup input box. Set yourRange = Application.InputBox( _ Prompt:="Select a range.", _ Title:="INPUT RANGE", _ Default:=Selection.Address, Type:=8) 'Type: Value Meaning '0 A Formula '1 A Number '2 Text (a string) '4 A logical value (True or False) '8 A cell reference, as a Range object '16 An error value, such as #N/A '64 An array of values On Error GoTo GetOut 'if no hard coded cells are found in the range, go to "GetOut" to exit Sub Set rangeNoFormula = yourRange.Cells.SpecialCells(xlCellTypeConstants) 'identifies cells without formulas rangeNoFormula.Interior.Color = 42495 GetOut: 'exits the sub End Sub 

范围有一个可以使用的名为HasFormula()的内置布尔函数。 你的代码看起来像这样(我也清理了一下):

 Public Sub hightlightNoFormulas() Dim yourRange as Range, rng as Range Set yourRange = Range("A1:A100") For Each rng in yourRange If Not rng.HasFormula Then rng.interior.Color = 65535 End If Next rng End Sub 

我从http://www.exceltrick.com/how_to/find-cells-containing-formulas-in-excel/得到了这个想法

这是另一个使用相同示例的解决scheme。 BGeorge的外表也是正确的。

 Public Sub hightlightNoFormulas() Dim yourRange As Range Dim rangeNoFormula As Range Dim rng As Range Set yourRange = Range("A1:A100") Set rangeNoFormula = yourRange.SpecialCells(xlCellTypeFormulas) For Each rng In yourRange If Intersect(rng, rangeNoFormula) Is Nothing Then rng.Interior.Color = 65535 Next rng End Sub