excel中的组的条件格式化

时间:2018-05-28 10:16:49

标签: excel conditional-formatting

想知道是否有办法为同一列中的不同组分别设置条件格式。像这样:

enter image description here

这个想法是应该为这些组独立完成色标。在组1中:2是最小值,因此将是红色,50是最高值,因此将是绿色(即使有像114或1467这样的值,它也不应该影响此范围,因为它属于不同的组)

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果组内的颜色缩放如下:

Public Sub FormatRanges()

    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng As Long
    Application.ScreenUpdating = False

    With ActiveSheet

        Set rng1 = .Range("B1:B5")
        Set rng2 = .Range("B6:B10")
        Set rng3 = .Range("B11:B15")
        Dim myRanges()
        myRanges = Array(rng1, rng2, rng3)

        For rng = LBound(myRanges) To UBound(myRanges)
           ApplyFormatting myRanges(rng)
        Next rng

    End With
    Application.ScreenUpdating = True
End Sub

Public Sub ApplyFormatting(ByRef rng As Variant)

     rng.FormatConditions.Delete
    rng.FormatConditions.AddColorScale ColorScaleType:=3
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    rng.FormatConditions(1).ColorScaleCriteria(1).Type = _
        xlConditionValueLowestValue
    With rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
        .Color = 7039480
        .TintAndShade = 0
    End With
    rng.FormatConditions(1).ColorScaleCriteria(2).Type = _
        xlConditionValuePercentile
    rng.FormatConditions(1).ColorScaleCriteria(2).Value = 50
    With rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
        .Color = 8711167
        .TintAndShade = 0
    End With
    rng.FormatConditions(1).ColorScaleCriteria(3).Type = _
        xlConditionValueHighestValue
    With rng.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 8109667
        .TintAndShade = 0
    End With
End Sub

示例数据:

Data

代码进入标准模块,按Alt + F11打开VBE,然后右键单击项目并添加标准模块。