Excel - Shading entire row based on change of value

后端 未结 11 1811
一生所求
一生所求 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:18

    In MS Excel, first save your workbook as a Macro Enabled file then go to the Developper Tab and click on Visual Basic. Copy and paste this code in the "ThisWorkbook" Excel Objects. Replace the 2 values of G = and C= by the number of the column containing the values being referenced.

    In your case, if the number of the column named "File No" is the first column (namely column 1), replace G=6 by G=1 and C=6 by C-1. Finally click on Macro, Select and Run it. Voila! Works like a charm.

    Sub color()
        Dim g As Long
        Dim c As Integer
        Dim colorIt As Boolean
    
        g = 6
        c = 6
        colorIt = True
    
        Do While Cells(g, c) <> ""
            test_value = Cells(g, c)
            Do While Cells(g, c) = test_value
                If colorIt Then
                    Cells(g, c).EntireRow.Select
                    Selection.Interior.ColorIndex = 15
                Else
                    Cells(g, c).EntireRow.Select
                    Selection.Interior.ColorIndex = x1None
                End If
                g = g + 1
            Loop
            colorIt = Not (colorIt)
        Loop
    End Sub
    

提交回复
热议问题