Using conditional formatting to alternate the formatting of data by category

后端 未结 3 1103
无人共我
无人共我 2021-01-14 09:24

I have a spreadsheet with a bunch of rows like this:

Name    |   ID    |   Category  |    Attributes...
-----------------------------------------------------         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 09:43

    You are going to want to setup two Conditional Formatting rules based upon a count unique type of formula like the following.

    =MOD(INT(SUMPRODUCT(1/COUNTIF($C$2:$C2, $C$2:$C2))), 2)
    =NOT(MOD(INT(SUMPRODUCT(1/COUNTIF($C$2:$C2, $C$2:$C2))), 2))
    

    The problem is that you do not want to extend that down with full column references since it uses the SUMPRODUCT function to provide array (or cyclic) type processing. Keeping track of the range it covers if rows and/or columns are added or removed becomes a problem.

    The solution is to have a sub procedure at the ready that can quickly create or renew the two CF rules for you based upon the Range.CurrentRegion property. That is the 'island' of data extending out until it meets a fully blank row or column.

    Sub Set_CF_rules()
    
        With Worksheets("Sheet1")
            With .Cells(1, 1).CurrentRegion
                With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                    .FormatConditions.Delete
    
                    .FormatConditions.Add Type:=xlExpression, Formula1:= _
                        "=MOD(INT(SUMPRODUCT(1/COUNTIF($C$2:$C2, $C$2:$C2))), 2)"
                    With .FormatConditions(.FormatConditions.Count).Interior
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent6
                        .TintAndShade = -0.249946592608417
                        .Parent.StopIfTrue = True
                    End With
    
                    .FormatConditions.Add Type:=xlExpression, Formula1:= _
                        "=NOT(MOD(INT(SUMPRODUCT(1/COUNTIF($C$2:$C2, $C$2:$C2))), 2))"
                    With .FormatConditions(.FormatConditions.Count).Interior
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent6
                        .TintAndShade = 0.799981688894314
                        .Parent.StopIfTrue = True
                    End With
    
                End With
            End With
        End With
    
    End Sub
    

    I've set a dark red and a pale red for the highlight 'striping' Experiment with the macro recorder while filling cells with varying colors, shades and patterns until you find something you want and replace the appropriate values in the above code.

            

提交回复
热议问题