Excel:基于这些行的最大值的一组行的格式颜色

时间:2015-06-27 01:39:25

标签: excel excel-vba conditional-formatting vba

我有以下问题。鉴于数据:

城市开始

1 2000-01-01

1 2002-02-01

1 2002-02-01

2 2002-02-01

2 2003-02-01

2 2005-12-01

3 2002-04-01

3 2003-07-01

3 2005-12-01

我想按照他们所属的城市对行进行分组。对于每个组,如果该组中的最大日期不等于2005-12-01,我想将该组中的所有行着色为红色,否则为蓝色。在示例数据中,这意味着城市1的所有行都显示为红色,而城市2和3的所有行都显示为蓝色。

有没有办法实现这个目标?谢谢!

1 个答案:

答案 0 :(得分:0)

非VBA方法:

  • 条件格式 - >新规则......
  • 选择"根据其值格式化所有单元格"
  • 格式样式="双色标度"
  • 颜色= [根据需要]

enter image description here

VBA方法:

Dim rng As Range

Set rng = ActiveSheet.Range("A1:A10") 'or where-ever.
With rng.FormatConditions
    .AddColorScale 2
    With .Item(1).ColorScaleCriteria(1)
        .Type = xlConditionValueLowestValue
        .FormatColor.Color = 15773696
    End With
    With .Item(1).ColorScaleCriteria(2)
        .Type = xlConditionValueHighestValue
        .FormatColor.Color = 255
    End With
End With

编辑:已经很晚了,所以在再次阅读这个问题之后,我可能会把颜色颠倒过来......强调"根据需要"。