无法使用类型调用FormatConditions.Add

时间:2014-03-04 10:25:28

标签: excel excel-vba vba

我正在以编程方式运行一个非常简单的FormatConditions ...

someVar的值为168.此代码在.FormatCondtions.Add

上失败
Set Sheet = Workbooks(1).Worksheets(1)
With Workbooks(1).Worksheets(1).Range("C2:C" & someVar)
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlDuplicate
    .FormatConditions(1).Interior.Color = RGB(255, 40, 80)
End With

我也试过使用Selection,错误保持不变

Workbooks(1).Worksheets(1).Range("C2:C" & someVar).Select
With Selection
   'See above
End With

1 个答案:

答案 0 :(得分:1)

试试这个:

Set Sheet = Workbooks(1).Worksheets(1)
With Sheet.Range("C2:C" & someVar)
    .FormatConditions.Delete
    .FormatConditions.AddUniqueValues
    .FormatConditions(1).DupeUnique = xlDuplicate
    .FormatConditions(1).Interior.Color = RGB(255, 40, 80)
End With