Excel - Shading entire row based on change of value

后端 未结 11 1788
一生所求
一生所求 2020-12-23 20:05

I would like to shade entire rows in Excel based on the value of one cell. For example say I have the rows below:

**File No**
1122
1122
1144
1155
1155
1155
         


        
11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 20:35

    I hate using these in-cell formulas and having to fill in a new column, and I finally learned enough to make by own VBA macro to accomplish this effect.

    This might not be all that logically different from another answer, but I think the code looks a hell of a lot better:

    Dim Switch As Boolean
    For Each Cell In Range("B2:B" & ActiveSheet.UsedRange.Rows.Count)
        If Not Cell.Value = Cell.Offset(-1, 0).Value Then Switch = Not (Switch)
        If Switch Then Range("A" & Cell.Row & ":" & Chr(ActiveSheet.UsedRange.Columns.Count + 64) & Cell.Row).Interior.Pattern = xlNone
        If Not Switch Then Range("A" & Cell.Row & ":" & Chr(ActiveSheet.UsedRange.Columns.Count + 64) & Cell.Row).Interior.Color = 14869218
    Next
    

    My code here is going by column B, it assumes a header row so it starts at 2, and I use the Chr(x+64) method to get column letters (which won't work past column Z; I haven't yet found a simple-enough method for getting past this).

    First, the boolean variable will alternate whenever the value changes to a new one (uses Offset to check cell above), and for each pass the row is checked for either True or False and colors it accordingly.

提交回复
热议问题