使用VBA进行条件格式化

时间:2012-01-25 14:53:42

标签: excel-vba vba excel

我想要使用条件格式的正确代码。我有4季度销售表格总和的数据(“K8:K207”)。我想应用条件格式,我有3个条件:

  1. 以绿色
  2. 突出显示年份大于1,00,000的K列(年度总销售额)
  3. 在90,000到1,00,000之间作为琥珀
  4. 并且红色不到90,000
  5. 请帮助我如何使用循环编写代码。

1 个答案:

答案 0 :(得分:11)

你不需要循环。您只需向范围对象添加新的FormatCondition即可。

lLow = 90000
lHigh = 100000

Set rng = Range("K8:K207")
rng.FormatConditions.Delete  ' delete any pre-existing formatting

' add greater than condition
With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & lHigh)
   .Interior.Color = rgbLimeGreen
End With

' add middle condition
With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=" & lLow, Formula2:="=" & lHigh)
   .Interior.Color = rgbGold
End With

' add less than condition
With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlLess, Formula1:="=" & lLow)
   .Interior.Color = rgbRed
End With
相关问题