【问题标题】:Highlight cells in a range that are "hard-coded" and not formula derived突出显示“硬编码”而不是公式派生的范围内的单元格
【发布时间】:2023-03-14 01:15:01
【问题描述】:

我在 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 中出现错误。我一直在寻找代码来突出显示可选范围内的单元格,这些单元格是“硬编码”而不是公式派生的(即单元格公式输入过多)。有人可以提供帮助吗?谢谢。

【问题讨论】:

  • 如果你把~xlCellTypeFormulas~改成~xlCellTypeAllFormatConditions.~或者~xlCellTypeSameFormatConditions.~对你有帮助吗。
  • 如果您使用 google vba specialcells 查看第一个响应,它会告诉您语法错误。 yourrange.specialcells(xlCellTypeFormulas)

标签: vba excel highlighting


【解决方案1】:

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

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

【讨论】:

  • 更简单,谢谢
【解决方案2】:

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

    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

【讨论】:

    【解决方案3】:

    范围有一个内置的布尔函数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/得到这个想法

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 谢谢。是的,BGeorge 的版本按预期工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多