VBA条件格式化单元格,其值大于或小于

时间:2017-02-27 21:09:34

标签: excel vba excel-vba conditional-formatting

我正在尝试编写一个宏代码来执行以下一系列单元格

  1. 条件格式仅适用于已输入值的单元格
  2. 如果单元格中的值小于$ I $ 6和/或大于$ M $ 6,则突出显示红色,如果不是,请不要突出显示(或不要应用)。
  3. 这用于对输入的数据进行规格检查,以确保数字在规格范围内。谢谢!

    我尝试了什么:

    Sub SpecCheck()
        Dim iRow As Range
        Set iRow = Range("f16:l34")
    
        For Each cell In iRow
            If cell.Value <> "" And cell.Value > "I6" And cell.Value < "M6" Then
                cell.Interior.Color = RGB(255, 0, 0)
            End If
        Next
    End Sub
    

    我试过的新代码,但没有用。也不确定是否重要,但代码是用&#34; general&#34;工作表的代码。

    Sub SpecCheck()
    
    Dim iRow As Range, cell As Range 
    Dim lowSpec As Range
    Dim highSpec As Range
    Set iRow = Range("f16:l34")
    Set lowSpec = Range("r6")
    Set highSpec = Range("s6")
    
    For Each cell In iRow
     If cell.Value <> "" And cell.Value > highSpec And cell.Value < lowSpec Then
        cell.Interior.Color = RGB(255, 0, 0)
     End If
    Next
    
    End Sub
    

1 个答案:

答案 0 :(得分:1)

您需要指定那些是范围,而不是字符串:

Sub SpecCheck()
Dim iRow As Range, cell as Range ' I also added the cell as Range
Set iRow = Range("f16:l34")

For Each cell In iRow
    If cell.Value <> "" And cell.Value > Range("$I$6").Value And cell.Value < Range("$M$6").Value Then
        cell.Interior.Color = RGB(255, 0, 0)
    End If
Next
End Sub