Color code duplicates with a twist

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
I know color code duplicates is a very common issue here on the internet, but I can't find any solution which is similar to my needs.

Based on the value in column A("My data") I want to color code the whole row where the value is the same.
I.e. row 1 and 2 has the same value and should be colored to grey. Row 3, 4 and 5 has the same value and should be colored to a another color and so on.

[TABLE="width: 220"]
<tbody>[TR]
[TD]My data[/TD]
[TD](entire row color)[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Grey[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Grey[/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]Dark grey[/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]Dark grey[/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]Dark grey[/TD]
[/TR]
[TR]
[TD]30[/TD]
[TD]Grey[/TD]
[/TR]
[TR]
[TD]30[/TD]
[TD]Grey[/TD]
[/TR]
[TR]
[TD]40[/TD]
[TD]Dark grey[/TD]
[/TR]
[TR]
[TD]40[/TD]
[TD]Dark grey[/TD]
[/TR]
</tbody>[/TABLE]

This is my code for the moment, but it doesn't work. The second if statement is major wrong.
Code:
LastRow = Cells(Rows.count, 1).End(xlUp).rowrowcolor = 225


For Each c In Range("A2:A" & LastRow)
 If c.Offset(1, 0).Value = c.Value Then
     Rows(c.row).EntireRow.Interior.Color = RGB(205, rowcolor, 205)
     Rows(c.row + 1).EntireRow.Interior.Color = RGB(205, rowcolor, 205)
 End If
   If rowcolor = 225 Then
     rowcolor = 255
      Else
     rowcolor = 225
   End If
Next c
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
How about
Code:
   lastrow = Cells(Rows.Count, 1).End(xlUp).Row
   rowcolor = 225
   
   For Each c In Range("A2:A" & lastrow)
      If c.Offset(1, 0).Value = c.Value Then
         Rows(c.Row).Resize(2).Interior.Color = RGB(205, rowcolor, 205)
      Else
         rowcolor = IIf(rowcolor = 225, 255, 225)
      End If
   Next c
 
Upvote 0
Why are you so smart? =) Thanks!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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