Highlight or flag duplicate values in the same cell

fredtriest

New Member
Joined
Nov 2, 2016
Messages
14
I was hoping someone could assist with a macro or formula that will help in highlighting or flagging a single cell that has duplicate values. I haven't had any luck with the macros I've found mostly due to them conflicting with the "and" or "&"

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Harvey, Mary, Kathy and Harvey[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]John and John[/TD]
[/TR]
</tbody>[/TABLE]

Thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This assumes there are only duplicates, no triplicates or beyond, and that sub-strings within a cell are comma-space or just space delimited. Select the cells you want to highlight first, then run the macro. Duplicates will be highlighted in bold red font.
Code:
Sub HiliteDupsInCellBoldRed()
'select the cells of interest first, then run this macro
Dim c As Range, Parts As Variant, i As Long, j As Long
Application.ScreenUpdating = False
For Each c In Selection
    If Not IsEmpty(c) And InStr(c.Value, " ") > 0 Then
        Parts = Split(Replace(c.Value, ",", ""), " ")
        For i = LBound(Parts) To UBound(Parts) - 1
            For j = i + 1 To UBound(Parts)
                If Parts(j) = Parts(i) Then
                    With c.Characters(InStrRev(c.Value, Parts(j)), Len(Parts(j)))
                        .Font.Bold = True
                        .Font.Color = vbRed
                    End With
                End If
            Next j
        Next i
    End If
Next c
Application.ScreenUpdating = True
End Sub
EDIT: Forgot to mention, this will only work on cells containing constants, not on cells with formulas in them.
 
Last edited:
Upvote 0
Hi ,

Another option :
Code:
Public Sub HighlightDuplicates()
           Dim lastrow As Long, i As Long
           Dim inputdatarange As Range, cell As Range
           Dim inputarray As Variant
           Dim cellval As String, currstring As String, currword As String, newword As String
           Dim matchpos1 As Integer, matchpos2 As Integer, lenpos1 as Integer


           On Error GoTo errorexit:
           
           With Application
                .ScreenUpdating = False
                .EnableEvents = False
           End With
           
           lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
           Set inputdatarange = ActiveSheet.Range("A1:A" & lastrow)
           
           For Each cell In inputdatarange
               cellval = cell.Value
               currstring = Replace(Replace(cellval, " and ", ","), " ", "")
               inputarray = Split(currstring, ",")
               
               For i = 0 To UBound(inputarray)
                   currword = inputarray(i)
                   matchpos1 = InStr(1, cellval, currword, vbTextCompare)
                   lenword1 = Len(currword)
                   For j = i + 1 To UBound(inputarray)
                       newword = inputarray(j)
                       If VBA.StrComp(currword, newword, vbTextCompare) = 0 Then
                          matchpos2 = InStr(matchpos1 + lenword1, cellval, newword, vbTextCompare)
                          
                          cell.Characters(matchpos1, lenword1).Font.Bold = True
                          cell.Characters(matchpos2, lenword1).Font.Bold = True
                          matchpos1 = matchpos2
                       End If
                   Next
               Next
           Next


errorexit:
           With Application
                .ScreenUpdating = True
                .EnableEvents = True
           End With
End Sub
 
Last edited:
Upvote 0
Both of these are great!! I hate to ask for more, but is it possible to take into consideration criteria such as:

Highlight a cell when names start with the first 3 letters? For example, it would catch "Dave" and "David" as a duplicate.
 
Upvote 0
Untested but this should highlight a word whose first 3 letters duplicate the first 3 letters of a word appearing earlier in the string within a cell. Select the cells for highlighting first, then run the macro.
Code:
Sub HiliteDupsInCellBoldRed3()
'select the cells of interest first, then run this macro
Dim c As Range, Parts As Variant, i As Long, j As Long
Application.ScreenUpdating = False
For Each c In Selection
    If Not IsEmpty(c) And InStr(c.Value, " ") > 0 Then
        Parts = Split(Replace(c.Value, ",", ""), " ")
        For i = LBound(Parts) To UBound(Parts) - 1
            For j = i + 1 To UBound(Parts)
                If Left(Parts(j), 3) = Left(Parts(i), 3) Then
                    With c.Characters(InStrRev(c.Value, Parts(j)), Len(Parts(j)))
                        .Font.Bold = True
                        .Font.Color = vbRed
                    End With
                End If
            Next j
        Next i
    End If
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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