使用VBA进行条件格式化

Rav*_*asi 5 excel vba excel-vba

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

  1. 突出显示年份大于1,00,000的列K(年度总销售额)为绿色
  2. 琥珀色在90,000到1,00,000之间
  3. 红色不到90,000

请帮助我如何使用循环编写代码.

mis*_*ab1 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
Run Code Online (Sandbox Code Playgroud)