Shade cells based on content

johnohio

New Member
Joined
May 12, 2005
Messages
48
Office Version
  1. 2016
Platform
  1. Windows
I am looking to create a spreadsheet that will color cells based on content.

Example...



So basically it will shade the cell color based on the value in column A.. changing when the customer # changes.




Thanks for your help!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Take a look at using Conditional Formatting, which allows you to format cells based on cell content. It can even reference other cells in the same row (i.e. you could Conditionally Format cell B1 based on the value in A1).
 
Upvote 0
This code will highlight every other customer number yellow.

Code:
Sub cf()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:C" & lr).ClearFormats
For x = 3 To lr
    If Cells(x, 1) <> Cells(x - 1, 1) And Cells(x - 1, 1).Interior.Color <> vbYellow Then
        Range(Cells(x, 1), Cells(x, 3)).Interior.Color = vbYellow
    End If
    
    If Cells(x, 1) = Cells(x - 1, 1) And Cells(x - 1, 1).Interior.Color = vbYellow Then Range(Cells(x, 1), Cells(x, 3)).Interior.Color = Cells(x - 1, 1).Interior.Color
Next x

End Sub
 
Upvote 0
This code will highlight every other customer number yellow.
It looks like this assumes that the Customer numbers are in column A starting in row 2 and this column is sorted.

Under those same assumptions, you can do the same thing with Conditional Formatting, not requiring any VBA.
Just highlight rows 2 down to the end, and enter this Conditional Formatting formula:
Code:
=ISODD(SUMPRODUCT(1/COUNTIF($A$2:$A2,$A$2:$A2)))
and choose your formatting color.
 
Upvote 0
Solution
It looks like this assumes that the Customer numbers are in column A starting in row 2 and this column is sorted.

Under those same assumptions, you can do the same thing with Conditional Formatting, not requiring any VBA.
Just highlight rows 2 down to the end, and enter this Conditional Formatting formula:
Code:
=ISODD(SUMPRODUCT(1/COUNTIF($A$2:$A2,$A$2:$A2)))
and choose your formatting color.


Thanks that is perfect!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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