2016-07-07 5 views
0

Ich habe derzeit diesen Code unten. Wenn die Zelle leer ist, ist die Füllung grün (4). Wenn die Zelle nicht leer ist, wird keine Füllung angezeigt. Aber was, wenn ich meine Zelle (0,3) mit gelb füllen möchte, wenn der Zellenwert = "JA"? Derzeit haben Sie ein Dropdown-Menü für "JA" oder "NEIN".So wenden Sie die Füllfarbe in Abhängigkeit vom Zellenwert an VBA

Private Sub worksheet_change(ByVal Target As Range) 


    If Not Intersect(Target, Range("A:A")) Is Nothing Then 


     ActiveSheet.Unprotect 
     If Target = "YES" Then 
      For i = 1 To 9 
       With Target.Offset(0, i) 
        .Locked = False 
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & Target.Offset(0, i).Address & ")" 
        With .FormatConditions(.FormatConditions.Count) 
         .SetFirstPriority 
         .Interior.ColorIndex = 4 
        End With 
       End With 
      Next i 

     '============================================================================================================== 
     '============================================================================================================== 

     ElseIf Target = "NO" Then 

       For i = 10 To 15 
       With Target.Offset(0, i) 
        .Locked = False 
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & Target.Offset(0, i).Address & ")" 
        With .FormatConditions(.FormatConditions.Count) 
         .SetFirstPriority 
         .Interior.ColorIndex = 4 
        End With 
       End With 
      Next i 

     Else 

       For i = 1 To 15 
        With Target.Offset(0, i) 
         .Value = "" 
         .Locked = True 
         .FormatConditions.Delete 
        End With 
       Next i 

     End If 

     ActiveSheet.Protect 

    End If 


End Sub 
+0

Es gibt einen Unterschied zwischen einer Zelle und einem Offset, die man meinen Sie ? (Es gibt auch keine 'Zelle (0,27)', der Index startet '1' –

Antwort

0

Dann Farbe verwenden = 65535, wenn range.value = "YES"

1

den Zusatz Siehe unten

For i = 26 To 28 

     With Target.Offset(0, i) 
      .Locked = False 
      .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & Target.Offset(0, i).Address & ")" 
      With .FormatConditions(.FormatConditions.Count) 
       .SetFirstPriority 
       .Interior.ColorIndex = 45 
      End With 
     End With 
     If i = 27 AND UCase(Target.Offset(0,i).value) = "YES" Then Target.Offset(0,i).Interior.Color = RGB(255,255,0) 
    Next i 
+0

Ich bin nicht sicher, warum es nicht auf meiner Seite funktioniert – PeterS

+0

@PeterS was funktioniert nicht? – RGA

+0

@PeterS ist der Wert in der Offset-Zelle (0,27) _exactly_ "YES"? – RGA

Verwandte Themen