条件格式错误

我试图写一些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 

任何build议,为什么会发生这种情况?

谢谢!

Range("C2:C")不是一个有效的范围,使其固定,或以下使其dynamic:

然后将您的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