Highlighting duplicates and triplicates

Trotter

New Member
Joined
May 2, 2020
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
I have a macro that works for duplicates with ranges that are not adjacent and with a tolerance value for the duplicate.



The second part of the macro is to highlight triplicates, which is where I am having problems.



This is one of many versions I have tried :





Sub Duplicates(r As Range)

Dim Cell

Application.ScreenUpdating = False

Set Dups1 = r

Set Dups2 = r



For Each c1 In Dups1

Duplicate = False

For Each c2 In r

If (c1.Address <> c2.Address) And c1.Value >= c2.Value And c1.Value < c2.Value + 5 Then

Duplicate = True

End If

Next c2

If Duplicate Then

c1.Interior.ColorIndex = 3

c1.Font.Color = vbWhite

Else

c1.Interior.ColorIndex = 19

c1.Font.ColorIndex = 1

End If

Next c1



For Each c2 In Dups2

Duplicate = False

For Each c1 In r

If (c1.Address <> c2.Address) And c1.Value >= c2.Value And c1.Value < c2.Value + 5 Then

Duplicate = True

End If

Next c1

If Duplicate Then

c2.Interior.ColorIndex = 3

c2.Font.Color = vbWhite

End If

Next c2

Application.ScreenUpdating = True

End Sub



Sub Triplicates(r As Range)

Dim Cell



Set Trips1 = r

Set Trips2 = r



For Each c1 In Trips1

Triplicate = False

For Each c2 In r

If c1.Interior.ColorIndex = c2.Interior.ColorIndex And (c1.Address <> c2.Address) And (c1.Value >= c2.Value) > 2 And c1.Value < c2.Value + 5 Then

Triplicate = True

End If

Next c2

If Triplicate Then

c1.Interior.ColorIndex = 10

End If

Next c1



For Each c2 In Trips2

Triplicate = False

For Each c1 In r

If c1.Interior.ColorIndex = c2.Interior.ColorIndex And (c1.Address <> c2.Address) And c1.Value >= c2.Value And c1.Value < c2.Value + 5 Then

Triplicate = True

End If

Next c1

If Triplicate Then

c2.Interior.ColorIndex = 10

End If

Next c2



End Sub



Sub Test()

ActiveSheet.Unprotect

Duplicates ActiveSheet.Range("C7:C12,J7:J12,Q7:Q12")

Triplicates ActiveSheet.Range("C7:C12,J7:J12,Q7:Q12")

ActiveSheet.Protect

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi and welcome to MrExcel!

I'm not sure what a "triplicate" is for you. In the following example, 10 is "duplicate" with 6 and also "duplicate" with 11.
So 10 is "triplicate"? and 6 and 11 only "duplicate"?

Dante Amor
ACJQR
1
211006
3101201000
4111401100
5301601130
6371801160
7442001190
8
Sheet2


If the above is correct, try this macro:

VBA Code:
Sub Duplicates_3()
  Dim c As Range, r As Range
  Dim dic1 As Object, dic2 As Object
  Dim j As Variant, ky As Variant
 
  Application.ScreenUpdating = False
  Set r = ActiveSheet.Range("C2:C7,J2:J7,Q2:Q7")
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
 
  r.Interior.ColorIndex = 19
  r.Font.Color = vbBlack
   
  For Each c In r
    dic2(c.Address) = c.Value
    For j = c.Value - 4 To c.Value + 4
      dic1(j) = dic1(j) + 1
    Next
  Next
  '
  For Each ky In dic2.keys
    j = dic1(dic2(ky))
    Select Case j
      Case 2
        Range(ky).Interior.ColorIndex = 3
        Range(ky).Font.Color = vbWhite
      Case Is >= 3
        Range(ky).Interior.ColorIndex = 10
        Range(ky).Font.Color = vbWhite
      Case 4
        '...
    End Select
  Next
End Sub

If all of the above is not correct, you could explain with examples when it is duplicated and when it is triplicated.
 
Upvote 0
I may have been unclear on my posting.
Duplicates and triplicates are defined in the macro posted.
As I said the macro for duplicates work. It is the additional formula to highlight triplicates that is a problem. It is the Triplicate part of the macro that is a problem.

Thanks for the reply
 
Upvote 0
With the macro I have the attached results.
It gives 6 defaults.
Everything have tried will change the defaults but still give 6 defaults
 

Attachments

  • Untitled.jpeg
    Untitled.jpeg
    24.5 KB · Views: 10
Upvote 0
I don't get replies on the forum, I only get a reply via email.
How do I correct this?
 
Upvote 0
The reason why I an using a macro is because the cells already have a formula to create them.
 
Upvote 0
As I said the macro for duplicates work. It is the additional formula to highlight triplicates that is a problem. It is the Triplicate part of the macro that is a problem.
In order to help you with "triplicates" I need to understand what a "triplicate" is for you.
Could you explain it with examples?

I don't get replies on the forum, I only get a reply via email.
How do I correct this?
Let's wait for a moderator to guide you.

With the macro I have the attached results.
It gives 6 defaults.
Everything have tried will change the defaults but still give 6 defaults
1589547802618.png
Which macro do you mean?
With that data sample, what are the correct results?
 
Upvote 0
Did you try my macro?

This is the result of my macro:
Dante Amor
ACDJKQRS
1
298D4060T
33522T80D
422T95D2
580D24T16D
6122D121D15D
763T14059T
8
Sheet2
 
Upvote 0
Hi Dante
Have tried your macro but get
Run-time error '429':
ActiveX component can't create object
It gets stuck on Set dic1 = CreateObject("Scripting.Dictionary")
 
Upvote 0
Hi Dante
Your macro works in Windows, but not on my Mac.
Any Ideas?
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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