用if条件格式化vba

时间:2017-08-08 12:11:47

标签: excel vba conditional-formatting

我有一个问题: 我正在尝试一次制作两种不同的条件格式。 但它只适用于第二个。 我使用 if Elseif ,仅使用 endif 我必须写些什么来兼顾他们的工作?第一个在那之后呢?

 For Each cell In Range("A1:AA1")

   If cell.Value = "GM WP6 Sensor Status" Then

Cells.FormatConditions.Delete
    Columns("H:H").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=32671"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    'Selection.FormatConditions(1).StopIfTrue = False
    Columns("H:H").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=LEN(TRIM(H1))=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .Pattern = xlNone
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("A1").Select

''''''''''''''''''''''''''Tikrina kita


ElseIf cell.Value = "GM WP6 Sensor Status light" Then

Cells.FormatConditions.Delete
   Columns("I:I").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
       Formula1:="=32767"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
       .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Columns("I:I").Select
   Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=LEN(TRIM(I1))=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .Pattern = xlNone
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("A1").Select
   End If
Next cell

1 个答案:

答案 0 :(得分:1)

请试一试......

Sub ConditionalFormatting()

Columns("H:H").FormatConditions.Delete
Columns("I:I").FormatConditions.Delete

If Application.CountIf(Range("A1:AA1"), "GM WP6 Sensor Status") > 0 Then
    Columns("H:H").FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(LEN(TRIM(H1))>0,H1<>32671)"
    Columns("H:H").FormatConditions(Columns("H:H").FormatConditions.Count).SetFirstPriority
    With Columns("H:H").FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
End If

If Application.CountIf(Range("A1:AA1"), "GM WP6 Sensor Status light") > 0 Then
    Columns("I:I").FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(LEN(TRIM(I1))>0,I1<>32671)"
    Columns("I:I").FormatConditions(Columns("I:I").FormatConditions.Count).SetFirstPriority
    With Columns("I:I").FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
End If
End Sub