Shading row to end of values based on change of va

2019-07-24 15:40发布

I don't know VBA well. Ivesleo answered an old post called, 'Shading entire column based on change of value'. Link to Shade Row Post

I used his code and it works but I don't know how to change the code to modify it for what I need.

  1. I have columns of weeks (column B) when we will schedule a project in certain cities (column C). Now the codes highlights the rows for every change in column B or (2nd column/week column). I need the code to note every change in the week within that city, so that when we go to another city, say Piley, it will not highlight the W1N4 for Piley b/c it first noticed that we are in a new city. Kind of like a custom sort with 2 levels, the city first, then the week. I don't want it to highlight based on the change in city, just note that there is a change in city and then highlight within that city based on the change in value in week column.
  2. How do you just shade row to the end of the data in that row? I tried End(xlRight).Select, End(xlLeft).Select, and End(xlUp).Row.Select but they didn't work.
  3. Is colorIt something he made up? I don't understand what it does and how Excel knows what to do with it? Again, I'm a beginner. Thank you for your help.

Week / City

W1N1    Silverton
W1N1    Silverton
W2N3    Silverton
W1N4    Silverton
W1N4    Piley
    Sub color()
    'Replace the 2 values of G = and C= by the number of the column containing _
    'the values being referenced.
    Dim g As Long
    Dim c As Integer
    Dim colorIt As Boolean

    g = 2
    c = 2
    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

1条回答
劳资没心,怎么记你
2楼-- · 2019-07-24 16:13

The following code has been tailored for your situation:

Sub color()
    Dim r As Long
    Dim colourIt As Boolean
    Dim colour As XlColorIndex

    colourIt = False

    With ActiveSheet
        r = 2  ' First row of data

        Do While .Cells(r, "B").Value <> ""
            'See if value has changed
            If .Cells(r, "B").Value <> .Cells(r - 1, "B").Value Or _
               .Cells(r, "C").Value <> .Cells(r - 1, "C").Value Then
                colourIt = Not colourIt
            End If

            'Determine which colour to use on this row
            If colourIt Then
                colour = 15
            Else
                colour = xlColorIndexNone
            End If
            'Apply the colouring
            .Range(.Cells(r, "B"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.ColorIndex = colour

            'Point to the next row of data
            r = r + 1
        Loop
    End With
End Sub

Note: The variable colourIt (was colorIt, but I'm Australian, so I'm going to use the English spelling!!) is simply a Boolean variable, which therefore can take the value True or False. The code is toggling between the two possible values (using the code colourIt = Not colourIt) each time the criteria being tested for changes.

Poster's Edit: Thank you so much. I just wanted to add here how I changed it to further fit my situation. I changed it as follows: I changed the color to a light orange using the RGB palette (I guess that is what it's called) and I changed .Range(.Cells(r, "B"),etc. to .Range(.Cells(r, "A"), etc. b/c it was highlighting the row beginning at column B vs. starting at column A.

I'm going to post a further thank you in the Comments.

 'Determine which colour to use on this row
        If colourIt Then
            colour = RGB(252, 228, 214)
        Else
            colour = xlColorIndexNone
        End If
        'Apply the colouring
        .Range(.Cells(r, "A"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.color = colour
查看更多
登录 后发表回答