VBA to highlight duplicate data

cmt_wright

New Member
Joined
Nov 23, 2007
Messages
8
Hi,
I have the following data in tables in a spreadsheet, and I need a vba script that looks at the whole sheet and highlights any fruit (and adjacent data) that appears more than once (note that the bottom table contains an additional column of data which should also be highlighted).
I also need it to highlight the fruit in a different colour depending on how many times it is duplicated.
For example, pear appears twice, so could be highlighted yellow, apple and orange appear three times each so they could be red.

apple 2
orange 3
pear 2
kiwi 4
mango 5


bannana 3
grape 2
peach 4
apple 2
orange 3


blueberry 2 A
coconut 2 B
orange 3 C
apple 2 D
pear 2 E



This is a small chunk of the data, but the full set will have 20 fruits and maybe 10 tables of data to compare.
At the moment I have started to write a script that has loops within loops within loops, but this isn't going to be feasible/efficient for my larger set of data.

Does anyone have some code that they can share which achieves this more efficiently?
(my vba experience is moderate)

Many thanks!!!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I would suggest you insert a column to the left, or use an empty column off to the right somewhere, and place a count of each fruit in the column.

Use a Dynamic range name - that makes it very easy to get the count. Now you're conditional format need only evaluate this count.

For multiple colors, that's a bit more to ask. With many colors, you'll need vba - perhaps you could use the count as the color index. Or just use VBA to assign a color of choice to each count - again, easier since you only need to look at the count value in your new column.

Hope this helps.

On dynamic named ranges:
http://www.contextures.com/xlNames01.html

------------------------------------------
Sample idea of this column I'm talking about (you'd probably hide the column in actuality).

Formula in Cell D1 is =COUNTIF(Fruit,A1) where fruit is a dynamic named range of column A. It actually may be a little off since there's empty cells between tables - you may need to tweak it a little.
book1
ABCD
1apple22
2orange32
3pear21
4kiwi41
5mango51
60
70
8bannana31
9grape21
10peach41
11apple22
12orange32
130
140
15blueberry2A1
16coconut2B0
17orange3C2
18apple2D2
19pear2E1
Sheet1
 
Upvote 0
VBA
try
Code:
Sub test()
Dim a, i As Long
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Value
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 1 To UBound(a, 1)
        If Not IsEmpty(a(i, 1)) Then
            If Not .exists(a(i, 1)) Then .item(a(i, 1)) = VBA.Array("", 0)
            w = .item(a(i, 1))
            w(0) = w(0) & IIf(w(0)="","",",") & Cells(i, 1).Address(0,0)
            w(1) = w(1) + 1 : .item(a(i, 1)) = w
        End If
    Next
    For Each e In .items
        If e(1) <> 1 Then
            Select Case e(1)
                Case 2 : myClr = vbYellow
                Case 3 : myClr = vbRed
                Case 4 : myClr = vbBlue
                Case 5 : myClr = vbMagenta
                Case 6 : myClr = vbGreen
                Case Else : myClr = vbCyan
            End Select
            Range(e(0)).Interior.Color = myClr
        End If
    Next
End With
End Sub
 
Upvote 0
Thanks Seiya for giving us a vba solution. I was posting back to say I have no idea why I used a dynamic named range - I think its because I added about 5 of them to a spreadsheet I was working on today. Just using the column would probably work for "Fruit" - i.e., =CountIf(A:A,A1) instead of =CountIf(Fruit,A1) Ah...time for sleep...
 
Upvote 0
You never said what version. This is built into Excel 2007.
 
Upvote 0
Thanks so much for the replies. Very helpful.

Seiya, for the script you wrote, this works great, but how would it be changed to use a named range.
i.e. if I wanted to compare for duplicates within range "Fruit1", and then wanted to apply the colour format to range "Fruit2" (a different range).

I am using excel 2003.

Again, thanks fo the help.
 
Upvote 0
try
Code:
Sub test()
Dim a, i As Long, w(), e
a = Range("fruit1").Value
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 1 To UBound(a, 1)
        If Not IsEmpty(a(i, 1)) Then
            If Not .exists(a(i, 1)) Then .item(a(i, 1)) = VBA.Array("", 0)
            w = .item(a(i, 1))
            w(0) = w(0) & IIf(w(0)="","",",") & Range("fruit1").Cells(i, 1).Address(0,0)
            w(1) = w(1) + 1 : .item(a(i, 1)) = w
        End If
    Next
    a = Range("fruit2").Value
    For i = 1 To UBound(a, 1)
        If Not IsEmpty(a(i, 1)) Then
            If Not .exists(a(i, 1)) Then .item(a(i, 1)) = VBA.Array("", 0)
            w = .item(a(i, 1))
            w(0) = w(0) & IIf(w(0)="","",",") & Range("fruit2").Cells(i, 1).Address(0,0)
            w(1) = w(1) + 1 : .item(a(i, 1)) = w
        End If
    Next    
    For Each e In .items
        If e(1) <> 1 Then
            Select Case e(1)
                Case 2 : myClr = vbYellow
                Case 3 : myClr = vbRed
                Case 4 : myClr = vbBlue
                Case 5 : myClr = vbMagenta
                Case 6 : myClr = vbGreen
                Case Else : myClr = vbCyan
            End Select
            Range(e(0)).Interior.Color = myClr
        End If
    Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,384
Messages
6,119,201
Members
448,874
Latest member
Lancelots

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