VBA Color duplicates with different colors

msmonster

New Member
Joined
Mar 21, 2022
Messages
1
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
I'm having a difficult time trying to adjust the code in VBA to find the duplicate ID numbers between two separate columns (A, B). There is over 17,000 lines in column B and 7700 lines in column A. Currently, the code is able to find the duplicates in a single column (B). The basic conditional format will not work in this scenario due to it only highlighting all duplicate versus matching individual duplicates.


Example Sheet:

External ID ID
900222111 900333222
900111222 900222111
900333222 900111222


Sub ColorDupsRGB()
Dim a As Variant, ky As Variant
Dim dic As Object, r As Range, i As Long, nColor As Long
Application.ScreenUpdating = False
Set r = Range("A3:T" & Range("B" & Rows.Count).End(xlUp).Row)
r.Interior.ColorIndex = xlNone
a = Application.Index(r.Value2, , 2)
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = vbTextCompare
For i = 1 To UBound(a)
dic(a(i, 1)) = dic(a(i, 1)) + 1
Next
nColor = 300000000
For Each ky In dic.keys
If dic(ky) > 1 Then
r.Offset(-1).AutoFilter 2, ky
ActiveSheet.AutoFilter.Range.Offset(1).Interior.Color = nColor
nColor = nColor + 1000000
End If
Next
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
r.Offset(r.Rows.Count).Resize(1).Interior.Color = xlNone
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
If you want a result like this:
1647877192589.png



Try the following:
VBA Code:
Sub ColorDupsRGB()
  Dim a As Variant, ky As Variant
  Dim dic As Object, r As Range, i As Long, nColor As Long
  
  Application.ScreenUpdating = False
  Set r = Range("A3:B" & Range("A" & Rows.Count).End(xlUp).Row)
  r.Interior.ColorIndex = xlNone
  a = r.Value2
  Set dic = CreateObject("Scripting.Dictionary")
  dic.CompareMode = vbTextCompare
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + 1
    dic(a(i, 2)) = dic(a(i, 2)) + 1
  Next
  nColor = 300000000
  For Each ky In dic.keys
    If dic(ky) > 1 Then
      r.Offset(-1).AutoFilter 1, ky
      ActiveSheet.AutoFilter.Range.Columns(1).Offset(1).Interior.Color = nColor
      ActiveSheet.Cells(1).AutoFilter
      r.Offset(-1).AutoFilter 2, ky
      ActiveSheet.AutoFilter.Range.Columns(2).Offset(1).Interior.Color = nColor
      ActiveSheet.Cells(1).AutoFilter
      nColor = nColor + 1000000
    End If
  Next
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  r.Offset(r.Rows.Count).Resize(1).Interior.Color = xlNone
  
  Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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