2016-11-10 42 views
0

我想寫一些vba代碼,將條件格式添加到工作表,但我不斷遇到應用程序定義的錯誤。下面是我的代碼條件格式錯誤

With sheet1.Range("C2:C") 
    .FormatConditions.Delete 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=NOT(ISBLANK($B2))" 
    .FormatConditions(1).Interior.ColorIndex = RGB(225, 242, 255) 
End With 

爲什麼這可能是發生什麼建議?

謝謝!

+1

沒有錯誤上會出現什麼線?糾正我,如果我錯了,但'範圍(「C2:C」)'不是一個有效的範圍? –

+0

你看起來像你指定的開始單元格行,但不包括最後一個單元格行..你在尋找一種方法來動態地添加該數字? – Clyde

+0

謝謝你是對的,我只是改變了範圍,包括最後一排。但是現在我得到了代碼的Interior.ColorIndex行的「下標超出範圍錯誤」? – ViggieSmalls

回答

1

Range("C2:C")不是一個有效的範圍內,使之固定,或以下使得動態:

然後更改您ColorIndex只是Color

With Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row) 
    .FormatConditions.Delete 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=NOT(ISBLANK($B2))" 
    .FormatConditions(1).Interior.Color = RGB(225, 242, 255) 
End With