VBA If Duplicate(s) Found then MsgBox One Time Only

rsmeyerson

Board Regular
Joined
Nov 29, 2014
Messages
104
Hello,

I would like help adjusting my code to open a message box if duplicate(s) are found in Column A. The code below opens a message box at each occurrence of a duplicate. I would like the message box to open only once if one or more duplicates are found. Thank you for your help.

VBA Code:
Sub HighlightDuplicates()
    Dim cel As Variant
    Dim myrng As Range
    Dim clr As Long
    Set myrng = Range("A2:A" & Range("A65536").End(xlUp).Row)
    myrng.Interior.ColorIndex = xlNone
    clr = 3
    
With Sheets("RtlBack")
        For Each cel In myrng
            If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
                MsgBox "Duplicate Found"
                If WorksheetFunction.CountIf(Range("A2:A" & cel.Row), cel) = 1 Then
                    cel.Interior.ColorIndex = clr
                    cel.Interior.TintAndShade = 0.6
                    clr = clr + 1
                Else
                    cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
                    cel.Interior.TintAndShade = 0.6
                End If
            End If
        Next
End With

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi rsmeyerson,

Try this:

Code:
Option Explicit
Sub HighlightDuplicates()
    Dim cel As Variant
    Dim myrng As Range
    Dim clr As Long
    Dim clnMyValues As New Collection
    Set myrng = Range("A2:A" & Range("A65536").End(xlUp).Row)
    myrng.Interior.ColorIndex = xlNone
    clr = 3
    
With Sheets("RtlBack")
        For Each cel In myrng
            If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
                On Error Resume Next
                    clnMyValues.Add cel, CStr(cel)
                    If Err.Number = 0 Then
                        MsgBox "Duplicate Found"
                    End If
                On Error GoTo 0
                If WorksheetFunction.CountIf(Range("A2:A" & cel.Row), cel) = 1 Then
                    cel.Interior.ColorIndex = clr
                    cel.Interior.TintAndShade = 0.6
                    clr = clr + 1
                Else
                    cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
                    cel.Interior.TintAndShade = 0.6
                End If
            End If
        Next
End With

End Sub

Regards,

Robert
 
Upvote 0
Thank you. In the sample sheet on which I'm running this code, there three separate sets of duplicates (each with two identical numbers). Your code results in three, successive openings of the message box (one at each duplicate). Is it possible to only open the message box when the first set of duplicates is identified?
 
Upvote 0
Maybe this:

Code:
Option Explicit
Sub HighlightDuplicates()
    Dim cel As Variant
    Dim myrng As Range
    Dim clr As Long
    Dim blnDupsFound As Boolean
    Set myrng = Range("A2:A" & Range("A65536").End(xlUp).Row)
    myrng.Interior.ColorIndex = xlNone
    clr = 3
    
With Sheets("RtlBack")
        For Each cel In myrng
            If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
                If blnDupsFound = False Then
                    MsgBox "Duplicate Found"
                    blnDupsFound = True
                End If
                If WorksheetFunction.CountIf(Range("A2:A" & cel.Row), cel) = 1 Then
                    cel.Interior.ColorIndex = clr
                    cel.Interior.TintAndShade = 0.6
                    clr = clr + 1
                Else
                    cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
                    cel.Interior.TintAndShade = 0.6
                End If
            End If
        Next
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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