Conditional Formatting - color groups based on row value

djangeroo

New Member
Joined
Nov 23, 2005
Messages
4
Hi,
I am new to the forum and impressed by the great advise that's out there.

This is my problem:
A list with numbers in column A, these numbers can be used over multiple rows, sometimes 3, 5 or more/less.
Every time this number changes, the color for the entire row should flip between yellow and no color.
Sequence of the number in column A is not always odd / even. I tried various suggestions regarding this topic but I do not get it straight.

Thanks for your help.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the Forum!

First, select all rows from Row 2 down that will be in your database.

Then go to Format|Conditional Formatting and Choose Formula Is from the drop Down.

In the formula box type =$A2<>$A1

Then click Format and select the Pattern Tab, choose Yellow

Then click Ok twice.
 
Upvote 0
Thanks but I have tried that, it does not work for me.

The way this formula is coloring rows for me is only the first row whenever the value in column A is changing. What I need is every time the value is the same in column A, the rows should have one color, if the value changes, the row should alternate color. Any suggestions are appreciated!
 
Upvote 0
Hello, djangeroo !
Welcome to the Board !!!!

conditional formatting for this would be very nice
can't seem to get the formula if possible

here is code which can do what you want

Code:
Option Explicit
Option Base 1

Sub color_same_data()
'Erik Van Geit
'051124 1754
'color cells with same contents in single column

Dim rng As Range
Dim LR As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim Arr As Variant
Dim ArrItem As String
Dim ArrRowNumbers() As Variant
Dim HL As Integer 'highlight

Const col = 1
Const FR = 2

If Cells(Rows.Count, col) <> "" Then LR = Rows.Count Else LR = Cells(Rows.Count, col).End(xlUp).Row
Set rng = Range(Cells(FR, col), Cells(LR, col))

Arr = rng.Value
i = 1
j = 1
    Do
    ArrItem = Arr(i, 1)
    k = i
        On Error Resume Next 'avoids bug at the end of the loop "Arr(i, 1)" when i > UBound(arr)
        Do
        i = i + 1
        Loop While ArrItem = Arr(i, 1)
        On Error GoTo 0
            'If k <> i - 1 Then     'enable these line to skip singles
            ReDim Preserve ArrRowNumbers(j + 1)
            ArrRowNumbers(j) = k + FR - 1
            ArrRowNumbers(j + 1) = i - 1 + FR - 1
            j = j + 2
            'End If                 'enable these line to skip singles
    Loop While i < LR

  
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With

For i = 1 To j - 1 Step 2
HL = IIf(HL = 35, 36, 35)
Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).Interior.ColorIndex = HL
Next i

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With

End Sub
kind regards,
Erik
 
Upvote 0
Yeah, this one is not as easy as it seems,

probably, Erik's solution is best for this situation.

There has got to be a way to do it with conditional formatting in Excel though...just gotta find it :)
 
Upvote 0
Thanks, Erik.
It works!
Now I am now wondering if it is possible to highlight the entire row.
And if it's possible to connect the run macro function with activating a cell.

Thanks again.
 
Upvote 0
Now I am now wondering if it is possible to highlight the entire row.
this line highlights the cells
Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).Interior.ColorIndex = HL
make it
Code:
Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).EntireRow.Interior.ColorIndex = HL
 
Upvote 0
...There has got to be a way to do it with conditional formatting in Excel though...just gotta find it :)

I knew there was a way...

Thanks to kcmuppet who recommended this site for conditional formatting:
http://www.mvps.org/dmcritchie/excel/condfmt.htm , I was able to manipulate one of the formulas to get the bands you needed.

Note: You have to create a helper column and house a 0 in the top cell above the table as in B1. You can always hide this row after you've entered the formulas.

Then insert formula in B2: =MOD(OFFSET($B2,-1,0)+ IF($A2<>OFFSET($A2,-1,0),TRUE,FALSE),2) and copy it down.

Then as for your conditional format, select all rows and in the Formula Is box, enter =$B1=1 and select the colour.
Book3
ABCD
10
2John1
3John1
4Jack0
5Jack0
6Marg1
7Bart0
8Maggie1
9Maggie1
10Del0
11Del0
Sheet2
 
Upvote 0
WOW, NBVC !!
nice to share this, thank you !!

I'm after this one since about 8 months, seems so easy when it's finished

the formula can be shortened to
Code:
=MOD(OFFSET($B2,-1,0) --($A2<>OFFSET($A2,-1,0)),2)
I wonder if we could find something without helper column :)

best regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,294
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top