2016-07-07 82 views
0

我目前有下面的代碼。如果單元格爲空白,則填充爲綠色(4)。當單元格不是空白時,將沒有填充。但是如果我希望我的單元格填充爲黃色(如果單元格值=「是」),則我的單元格格式爲(0,3)?目前有一個「是」或「否」的下拉菜單。如何應用填充顏色取決於單元值使用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

的另外還有一種細胞和偏移,其中一個意思之間的差異? (另外,沒有'Cell(0,27)',索引開始'1' –

回答

0

然後使用Color = 65535如果range.value = 「YES」

1

見下

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

我不知道爲什麼它不在我身邊 – PeterS

+0

@PeterS什麼不工作? – RGA

+0

@PeterS是偏移單元格中的值0,27)_exactly_「YES」? – RGA

相關問題