Enter duplicates multiples columns Highlighting with different colors with VBA

Goalexcel

Board Regular
Joined
Dec 28, 2020
Messages
101
Office Version
  1. 2016
Platform
  1. Windows
Data in sheet1 start A2 until J or more columns need to highlight with different colors.
Each color represent the number duplicate that start in column N and more columns.
When number is repeated for third time will change the color with the new color.
Code need to be flexible every time enter a new duplicate, and run with active workbook, and active worksheet
Add comments with text: Repeated N, O
If possible, use light colors.

Please see below image with result looking for
1638745459116.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
VBA Code:
Sub test()
Application.ScreenUpdating = False
Dim rB As Range
Dim k, Lr, Lc, color As Long
Dim Str, c As String
Dim Dic As Object
Set Dic = CreateObject("Scripting.Dictionary")
Set rB = Range("N2").CurrentRegion
Lr = Cells(Rows.Count, 1).End(xlUp).Row
Lc = Cells(Lr, Columns.Count).End(xlToLeft).Column
With Range("A1", Cells(Lr, Lc))
.Interior.color = xlNone
.ClearComments
End With
    For Each cell In rB
        color = cell.Interior.color 'read the Color
        Str = "@" & cell.Value & "@" & Cells(1, cell.Column)
        If Not Dic.exists(Str) And cell <> "" Then
        Dic.Add Str, color                              ' create a unique list: @13250@N,...,@6255@N ,@6255@O
        End If
    Next
    For Each cell In Range("A1", Cells(Lr, Lc))
        c = ""
        k = 0
        For Each Key In Dic.keys
            If InStr(1, Key, "@" & cell & "@") > 0 Then 'loop each cell, check if value exist in uniquelist
            cell.Interior.color = Dic.Item(Key)         ' then write color down
            c = c & Split(Key, "@")(2) & ","            'read duplicate value then write column header
            k = k + 1
            End If
        Next
        If k > 1 Then cell.AddComment "Repeat " & c 'write the comment
    Next
Application.ScreenUpdating = False
End Sub


Screenshot 2021-12-06 121010.png
 
Upvote 0
Solution
@bebo021999 Thank you for your replay. Please kindly check the below file, after I run the code only appear comments, but not colors.

 

Attachments

  • Untitled.png
    Untitled.png
    28.3 KB · Views: 13
Upvote 0
The code is trying to read color from N:R range.
There is no background color set to that range.
Try to set color as display in your 1st image then it should works..
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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