Using conditional formatting to alternate the formatting of data by category

后端 未结 3 1077
无人共我
无人共我 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:42

    I don't believe this is easily done with the built in tools, however you can add a calculated column, then apply conditional formatting based on it.

    This example assumes that your data is sorted by the category column. If you are not sorting the table by the category column, you will need one more step, which is to create a lookup table where you calculate the TRUE/FALSE COL_STRIPE value, then in your COL_STRIPE column, perform a VLOOKUP of it.

    Here is a working example

    1. Add the Calculated Column
      • Add a new column called COL_STRIPE, in my example this is column E.
      • Add the following formula to the column: =IF(C2<>C1,IFERROR(NOT(E1),FALSE),E1).
      • This column has the value true or false.
      • If the category is the same as the previous row, the COL_STRIPE value remains the same, otherwise it is negated.
      • In the case of the first row, the IFERROR() statement will set it to false.
      • Copy this formula down the column.
    2. Apply The Conditional Format
      • Highlight your table
      • Go to the Home Ribbon > Conditional Formatting > Highlight Cells Rules > More Rules
      • Use a formula to determine which cells to format: =NOT($E2), this will apply the format to all rows for which the E column (COL_STRIPE) is FALSE
      • Click the format button to set your desired background color.
    0 讨论(0)
  • 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.

            

    0 讨论(0)
  • 2021-01-14 09:48

    Please select ColumnsA:D and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::

    =ISEVEN(SUMPRODUCT(1/COUNTIF($C$1:$C1,$C$1:$C1)))
    

    Format..., select your choice of formatting (dark?), OK, OK.

    For 'light' I have assumed that either no fill is light enough or, if not, you apply standard fill to suit (CF, where triggered, will override it).

    The COUNTIF is a sequential counter that starts at 1 for each category. Dividing that into 1 and applying SUMPRODUCT generates a sequential counter by category (only changes when the category does). ISEVEN is applied to pick alternate categories. To investigate the detailed workings of formulae M$ has provided Evaluate Formula.

    0 讨论(0)
提交回复
热议问题