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?
 
Could be anything up to about 150, 000 rows.
Well, please try it on a small sample first. It it works then try it on your actual data. If it takes too long, we'll try to amend the code to work faster.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
About how many rows is your data in col C?
Another option to try:
VBA Code:
Sub Dazzawm_1()
'' 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
c.Interior.Color = vbYellow
Debug.Print "It's done in:  " & Format(Timer - t, "0.00") & " seconds"
End Sub

Book1
BCDADAEAF
1
2Ga
3Gb
4Ja
5Kb
6Ma
7Ma
8Mb
9Sa
10Sb
11Sc
12Ua
13Ub
14Ua
15
Sheet1
Very odd, I got this error

1691580465477.png


And when I debug it points to

c.Interior.Color = vbYellow
 
Upvote 0
Very odd, I got this error

And when I debug it points to

c.Interior.Color = vbYellow

If you try it on a small sample, make sure there are duplicates in col AE.
 
Upvote 0
If you try it on a small sample, make sure there are duplicates in col AE.
It appears to bring up that error when there are no duplicates in AE. Any chance the whole row could be coloured, not just C please?
 
Upvote 0
It appears to bring up that error when there are no duplicates in AE. Any chance the whole row could be coloured, not just C please?
Just change:
c.Interior.Color = vbYellow
to:
c.EntireRow.Interior.Color = vbYellow

I tested the code with 150K rows of data, and it took 224 seconds. If this is a one-time task, then that should be sufficient. However, if this is a recurring task, I can attempt to modify the code.
 
Upvote 0
Just change:
c.Interior.Color = vbYellow
to:
c.EntireRow.Interior.Color = vbYellow

I tested the code with 150K rows of data, and it took 224 seconds. If this is a one-time task, then that should be sufficient. However, if this is a recurring task, I can attempt to modify the code.
I will be using it on multiple files of varying sizes so up to you. Instead of that error coming up could a message box pop up saying no duplicates found in AE, something like that please?
 
Upvote 0
Please find the working sample:
I also altered the code a bit:
VBA Code:
Sub test()
  Dim d As Object, columnC As Variant, columnAE As Variant, i As Long, j As Long, counter As Long, s As Long
  Set d = CreateObject("Scripting.Dictionary")
  With Application
  columnC = .Transpose(Intersect(UsedRange, Columns("C")))
  columnAE = .Transpose(Intersect(UsedRange, Columns("AE")))
  .ScreenUpdating = False
 
  For i = 2 To UBound(columnC)
    If columnC(i) = columnC(i - 1) Then
      d.Add columnAE(i - 1), 1
      If d.Exists(columnAE(i)) Then
      counter = 0
        For j = s To UBound(columnC)
          If columnC(s) = columnC(j) Then
            counter = counter + 1
          Else
            Exit For
          End If
        Next
        Cells(s, 3).Resize(counter).Interior.Color = 65535
        d.RemoveAll
        i = j
        s = i
      End If
    Else
      s = i
      d.RemoveAll
    End If
  Next

  .ScreenUpdating = True
  End With
End Sub
 
Upvote 0
I will be using it on multiple files of varying sizes so up to you. Instead of that error coming up could a message box pop up saying no duplicates found in AE, something like that 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
 
Upvote 0
Please find the working sample:
I also altered the code a bit:
VBA Code:
Sub test()
  Dim d As Object, columnC As Variant, columnAE As Variant, i As Long, j As Long, counter As Long, s As Long
  Set d = CreateObject("Scripting.Dictionary")
  With Application
  columnC = .Transpose(Intersect(UsedRange, Columns("C")))
  columnAE = .Transpose(Intersect(UsedRange, Columns("AE")))
  .ScreenUpdating = False
 
  For i = 2 To UBound(columnC)
    If columnC(i) = columnC(i - 1) Then
      d.Add columnAE(i - 1), 1
      If d.Exists(columnAE(i)) Then
      counter = 0
        For j = s To UBound(columnC)
          If columnC(s) = columnC(j) Then
            counter = counter + 1
          Else
            Exit For
          End If
        Next
        Cells(s, 3).Resize(counter).Interior.Color = 65535
        d.RemoveAll
        i = j
        s = i
      End If
    Else
      s = i
      d.RemoveAll
    End If
  Next

  .ScreenUpdating = True
  End With
End Sub
Still the same error
 
Upvote 0
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
That seemed to work fine on a small file.
 
Upvote 0

Forum statistics

Threads
1,215,832
Messages
6,127,150
Members
449,366
Latest member
reidel

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