Excel - Macro to highlight Rows with Duplicate values in a cell

Solved/Closed
ajaydwivediji Posts 9 Registration date Tuesday October 30, 2012 Status Member Last seen January 5, 2017 - Oct 30, 2012 at 06:26 AM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Jan 19, 2015 at 12:06 PM
Geetings,

Kindly help us in creating a macro that highlight rows based on duplicate values in a cell [this cell may contain Alphabets, Numbers or alphanumerics].

For example -

Column A | Column B
Ajay | 001
AJ | 001
VJ | 002
Vijay | 002
SJ | 003
Sanjay | 003

Note: The rows containing the value [001] on Column B should be highlighed in [Light Turquoise color scheme], and the rows containing value [002] on Column B should be highlighted in [Grey-25% color scheme].
Similarly for the rows containing value as [003] should be highlighed again in [Light Turquoise color scheme]

The patern should be same for remaining duplicate entired in entire sheet i.e.
[Light Turquoise color scheme]
[Grey-25% color scheme]
...
..
.

The assistance on this request would be highly appreciated.

Thank you and Best Regards - AD


7 responses

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Aug 22, 2013 at 10:29 AM
Hi Stalin,

Take a look at the following sample data (column B):
abc
abc
def
ghi
ghi
ghi
ghi
jkl

If you want the result to be 2 (abc and ghi are counted as duplicates) then the following code will do the trick:
Sub test()
Dim x, cDup, lRow As Integer, skipDup As String
lRow = Range("B1").End(xlDown).Row
x = 1
cDup = 0
Do
    x = x + 1
    If Cells(x, "B").Value = Cells(x - 1, "B").Value And skipDup <> Cells(x, "B").Value Then
        cDup = cDup + 1
        skipDup = Cells(x, "B").Value
    End If
Loop Until x = lRow + 1
MsgBox "Column B contains " & cDup & " Duplicates."
End Sub

If this isn't what you meant, then please provide some sample data of your own along with the desired result.

Best regards,
Trowa

4