我想要使用条件格式的正确代码。我有4季度销售表格总和的数据(“K8:K207”)。我想应用条件格式,我有3个条件:
请帮助我如何使用循环编写代码。
答案 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