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

时间:2016-06-10 17:43:42

标签: excel vba excel-vba highlighting

我在Stackoverflow上发现了这段代码VBA (highlight Hardcode cell (i.e.1234) in Excel) after model is built

 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

虽然 "Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas" 部分在Excel 2010中出错。我一直在寻找能够突出显示可选区域中的单元格的代码,这些单元格是硬编码的"而不是公式衍生的(即细胞配方被过度分型)。有人可以提供帮助吗?感谢。

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

我结束了修改此代码并添加了一个输入框以选择我想要检查的范围。该代码非常适合我的需求。谢谢大家!

    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

答案 2 :(得分:0)

Ranges有一个名为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/

得到了这个想法

答案 3 :(得分:0)

这是使用相同示例发布的另一种解决方案。 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