G
guy
I have some product codes in 3 columns that are sorted. I would like to alternate the color of rows
when a product code changes. How done?
when a product code changes. How done?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Additional column, assume A.
Assume your data is now in Col B and is sorted on Col B such that changes in
Col B denote required change of colour.
In A2 =--(B2<>B1)
In A3 =(B3<>B2)+A2 and copy down as far as your data goes
Select the entire sheet, do Format / Conditional Formatting, change cell
value is to Formula is and put in
=MOD($A1,2)=0
Choose a colour from the pattern tab on the format dialog box and hit OK

My last post covers the three columns instead of just oneHere it is in code if you are using conditional formats already for a
different purpose. You will probably want to cahnge the colours which just
requires changing intCoulour1 and intColour2. These should really be
constants anyway...
Sub test()
Dim rngToColour As Range
Dim varLastValue As Variant
Dim intColour1 As Integer
Dim intColour2 As Integer
Dim intCurrentColour As Integer
intColour1 = 2
intColour2 = 3
intCurrentColour = intColour1
Set rngToColour = Sheet1.Range("A2:C2")
varLastValue = rngToColour.Value(1, 1)
Do While rngToColour.Value(1, 1) <> ""
rngToColour.Interior.ColorIndex = intCurrentColour
If rngToColour.Value(1, 1) <> varLastValue Then
If intCurrentColour = intColour1 Then
intCurrentColour = intColour2
Else
intCurrentColour = intColour1
End If
varLastValue = rngToColour.Value(1, 1)
End If
Set rngToColour = rngToColour.Offset(1, 0)
Loop
End Sub
HTH
A2 =--(B2&C2&D2<>B1&C1&D1)
A3 =(B3&C3&D3<>B2&C2&D2)+A2 and copy down
Hide Col A and you won't even know it's there
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.