Exit Sub when two duplicates values found

tobysimon

New Member
Joined
Jun 14, 2024
Messages
11
Office Version
  1. 2021
Hello Experts,

Am trying to execute a code where if Column "B" as more than 1 duplicate value in each cell then a msgbox "Duplicate Entry Made" would pop and exits the Sub. But if no duplicate found it will run the entire script. I've tried Evaluation.Countif.Unique but nothing seems to work.

Thank you in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Suggest you check out the answer posted here by "Tim" that uses the dictionary. Among the advantages, it's very fast.

VBA Code:
Sub ColumnDuplicates()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long

lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
Set oDictionary = CreateObject("Scripting.Dictionary")
For iCntr = 1 To lastRow
    If Cells(iCntr, 2) <> "" Then
        If oDictionary.Exists(Cells(iCntr, 2).Value) Then
            MsgBox ("Duplicate entry made.")
            Exit Sub
        Else 
            oDictionary.Add Cells(iCntr, 2).Value, Cells(iCntr, 2).Value
        End If
    End If
Next
MsgBox ("No Duplicates in Column B")
End Sub
 
Upvote 0
Other option,
Code:
    Dim s$
    s = Range("b2", Range("b" & Rows.Count).End(xlUp)).Address
    If Evaluate("counta(" & s & ")>counta(unique(" & s & "))") Then
        MsgBox "Duplicate Entry Made": Exit Sub
    End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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