VBA to find duplicate values in two columns

rhmkrmi

Active Member
Joined
Aug 17, 2012
Messages
341
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I need a VBA to compare two columns and show a message with the number of duplicates found and then stop the user from progressing until the duplicate is made unique.
If the VBA can also highlight the duplicates, it can save me some conditional formatting too.

Example:
1609917991386.png


Thank you.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi rhmkrmi
I went by your example and assumed there wouldn't be 2 duplicates in the second range
this checks each value in first range and tries to find a duplicate in the second, if found it sets the font both red

Code:
Sub Validation()
    Dim FirstRng, SecondRng As Range
    Set FirstRng = Sheet1.Range("A:A")
    Set SecondRng = Sheet1.Range("B:B")
   
    If CheckForDups(FirstRng, SecondRng) = True Then
        MsgBox "There were duplicates found and they have been highlighted"
        Exit Sub
    End If
End Sub

Function CheckForDups(ByVal CycleRng As Range, ByVal CheckRng As Range) As Boolean
    Dim cell As Range
    Dim Duplicate As Range
    For Each cell In CycleRng.Cells
        If cell.Value = "" Then Exit For
        Set Duplicate = CheckRng.Find(cell.Value)
        If Not Duplicate Is Nothing Then
            'Duplicate found
            cell.Font.Color = vbRed
            Duplicate.Font.Color = vbRed
            CheckForDups = True
        End If
    Next cell
End Function
[code]
 
Upvote 0
Thank you E.

There may actually be more than one duplicate in both ranges and I also have a condition which I missed in my original post, sorry!
It should look for duplicates only if column C is populated with Shop.

Can we make the code run automatically? I have to manually run the macro now.
It also does not set the font color back to Automatic when I fix the duplication.
I have a bunch of other macros for the same sheet as Private Sub Worksheet_Change(ByVal Target As Range) and tried to include your code in there but it fails.

1609975525508.png


Thank you.
 
Upvote 0
I have fixed all the issues in the code below.
As for triggering this event you need to reference this code in the Private Sub Worksheet_Change(ByVal Target As Range) by putting in Validation()
some where in there

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Validation()

'other code

End Sub

I'm not sure a function can work in there so referencing it is the best solution




Code:
Sub Validation()
    Dim FirstRng, SecondRng As Range
    Set FirstRng = Sheet1.Range("A:A")
    Set SecondRng = Sheet1.Range("B:B")
   
    If CheckForDups(FirstRng, SecondRng) = True or CheckForDups(SecondRng, FirstRng) = True Then 'run both ranges through to get dups
        MsgBox "There were duplicates found and they have been highlighted"
        Exit Sub
    End If
End Sub

Function CheckForDups(ByVal CycleRng As Range, ByVal CheckRng As Range, First As Boolean) As Boolean

    Dim cell As Range

    Dim Duplicate As Range
    If First = True Then
        CycleRng.Font.Color = vbBlack
        CheckRng.Font.Color = vbBlack
    End If
    For Each cell In CycleRng.Cells

        If cell.Value = "" Then Exit For

        Set Duplicate = CheckRng.Find(cell.Value)

        If Not Duplicate Is Nothing And cell.Parent.Cells(cell.Row, 3) = "Shop" Then

            'Duplicate found

            If cell.Parent.Cells(cell.Row, 3) = "Shop" Then cell.Font.Color = vbRed

            If Duplicate.Parent.Cells(Duplicate.Row, 3) = "Shop" Then Duplicate.Font.Color = vbRed

            CheckForDups = True

        End If

    Next cell

End Function
 
Upvote 0
I have fixed all the issues in the code below.
As for triggering this event you need to reference this code in the Private Sub Worksheet_Change(ByVal Target As Range) by putting in Validation()
some where in there

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Validation()

'other code

End Sub

I'm not sure a function can work in there so referencing it is the best solution




Code:
Sub Validation()
    Dim FirstRng, SecondRng As Range
    Set FirstRng = Sheet1.Range("A:A")
    Set SecondRng = Sheet1.Range("B:B")
  
    If CheckForDups(FirstRng, SecondRng) = True or CheckForDups(SecondRng, FirstRng) = True Then 'run both ranges through to get dups
        MsgBox "There were duplicates found and they have been highlighted"
        Exit Sub
    End If
End Sub

Function CheckForDups(ByVal CycleRng As Range, ByVal CheckRng As Range, First As Boolean) As Boolean

    Dim cell As Range

    Dim Duplicate As Range
    If First = True Then
        CycleRng.Font.Color = vbBlack
        CheckRng.Font.Color = vbBlack
    End If
    For Each cell In CycleRng.Cells

        If cell.Value = "" Then Exit For

        Set Duplicate = CheckRng.Find(cell.Value)

        If Not Duplicate Is Nothing And cell.Parent.Cells(cell.Row, 3) = "Shop" Then

            'Duplicate found

            If cell.Parent.Cells(cell.Row, 3) = "Shop" Then cell.Font.Color = vbRed

            If Duplicate.Parent.Cells(Duplicate.Row, 3) = "Shop" Then Duplicate.Font.Color = vbRed

            CheckForDups = True

        End If

    Next cell

End Function
Thank you.

I tried to run it individually in a new workbook and get this error:

1609981954694.png
 
Upvote 0
Hi
Looks like I missed a spot.
Make this line
If CheckForDups(FirstRng, SecondRng) = True or CheckForDups(SecondRng, FirstRng) = True Then

Say
If CheckForDups(FirstRng, SecondRng, True) = True or CheckForDups(SecondRng, FirstRng, False) = True Then
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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