2017-08-08 248 views
0

我有一個問題: 我試圖做兩個不同的條件格式一次。 但它只適用於第二個。使用 林如果ELSEIF,只有ENDIF 工作我必須寫得到解決它們兩者兼而有之?第一個之後呢?條件格式化vba與if

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

請試試這個...

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 
+0

運行時錯誤5無效的過程調用或參數。在此:列(「H:H」)。FormatConditions.Add類型:= xlExpression,Formula1:= _ 「= AND(LEN(TRIM(H1))> 0,H1 <> 32671)」 –

+0

我做一些更正它現在的工作:)謝謝@sktneer –

+0

@SkirmanteValentaite不客氣! :) – sktneer