Code To Find Duplicates And Highlight

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
Is there a code or formula for when there is matching cells in column C that will then look in column AE and highlight any duplicate values please?
 
Have you tried it on your actual data? How long did it take?
Try this one to pop up a message when there's no duplicate:
VBA Code:
Sub Dazzawm_2()
'' section group looping
Dim i As Long, j As Long, n As Long
Dim c As Range, a
Dim t As Double
t = Timer - t
Range("C:C").Interior.Color = xlNone
n = Range("C" & Rows.Count).End(xlUp).Row

For i = 2 To n
    If Len(Cells(i, "C")) > 0 Then
        j = WorksheetFunction.CountIf(Range("C" & i & ":C" & n), Cells(i, "C")) 'case insensitive
'            Debug.Print Cells(i, "A").Resize(j).Address
            a = WorksheetFunction.Unique(Cells(i, "AE").Resize(j))
            If UBound(a) < j Then
                If c Is Nothing Then
                    Set c = Cells(i, "C").Resize(j)
                Else
                    Set c = Union(c, Cells(i, "C").Resize(j))
                End If
            End If
            i = i + j - 1
    End If
Next

If Not c Is Nothing Then
    c.EntireRow.Interior.Color = vbYellow
Else
    MsgBox "No duplicates found"
End If
Debug.Print "It's done in:  " & Format(Timer - t, "0.00") & " seconds"
End Sub
BTW the timer didn't pop up
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Have you tried it on your actual data? How long did it take?
Try this one to pop up a message when there's no duplicate:
VBA Code:
Sub Dazzawm_2()
'' section group looping
Dim i As Long, j As Long, n As Long
Dim c As Range, a
Dim t As Double
t = Timer - t
Range("C:C").Interior.Color = xlNone
n = Range("C" & Rows.Count).End(xlUp).Row

For i = 2 To n
    If Len(Cells(i, "C")) > 0 Then
        j = WorksheetFunction.CountIf(Range("C" & i & ":C" & n), Cells(i, "C")) 'case insensitive
'            Debug.Print Cells(i, "A").Resize(j).Address
            a = WorksheetFunction.Unique(Cells(i, "AE").Resize(j))
            If UBound(a) < j Then
                If c Is Nothing Then
                    Set c = Cells(i, "C").Resize(j)
                Else
                    Set c = Union(c, Cells(i, "C").Resize(j))
                End If
            End If
            i = i + j - 1
    End If
Next

If Not c Is Nothing Then
    c.EntireRow.Interior.Color = vbYellow
Else
    MsgBox "No duplicates found"
End If
Debug.Print "It's done in:  " & Format(Timer - t, "0.00") & " seconds"
End Sub
Sorry to be a pain but could it exclude some values in AE? I would like NDA, NLA, NA and NYA excluded when looking in AE please. But it does show me the code works great. Sorry I didn't think of this when posting.
 
Upvote 0
BTW the timer didn't pop up
Debug.Print return the result in the Immediate Window (in vba editor).

Sorry to be a pain but could it exclude some values in AE? I would like NDA, NLA, NA and NYA excluded when looking in AE.
I'll try.
 
Upvote 0
It is as I have it in other codes. But thanks very much for your efforts. @Akuini seems to have nailed it.
Very strange indeed... I am sure I've nailed it also. The code handles 30000 lines in a blink of an eye.

Just for my curiosity, when you run it in the sample file do you get the error?
At which line? What does error states?
 
Upvote 0
Very strange indeed... I am sure I've nailed it also. The code handles 30000 lines in a blink of an eye.

Just for my curiosity, when you run it in the sample file do you get the error?
At which line? What does error states?
Sample file says deleted. It didn't give a line just errored straight away.
 
Upvote 0
Somebody must have downloaded it before you. Here find wetransfer link:
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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