0

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
0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Run time error 5. Invalid procedure call or argument . At this : Columns("H:H").FormatConditions.Add Type:=xlExpression, Formula1:= _ "=AND(LEN(TRIM(H1))>0,H1<>32671)"
i make some corrections its working now :) thanks @sktneer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.