Skirmante Valentaite
Skirmante Valentaite

Reputation: 105

conditional formatting vba with if

I have a question: I'm trying to make two different conditional formatting at once. But it works only the second one. Im using if and Elseif, and working only endif What i have to write to get work them both? First one after that another?

 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

Upvotes: 0

Views: 1275

Answers (1)

Subodh Tiwari sktneer
Subodh Tiwari sktneer

Reputation: 9966

Please give this a try...

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

Upvotes: 1

Related Questions