VBA search for duplicates & display warning message

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
I have the following code. Essentially, looking to find any duplicates in column B. The problem, is it continues to duplicate the function. Basically, what I want it to do is once it finds a duplicate in sheet names "Control Sheet" then display message and stop. No need to continue looping. Is this possible?

Code:
   Range("B1").Select    Do While ActiveCell.Value <> ""
        vtnaddress = ActiveCell.Address
        vtn = ActiveCell.Value
        Range("B1").Select
        Do Until ActiveCell.Address = ""
            If ActiveCell.Value = vtn Then
                MsgBox "Duplicate fund found, please check fund list"
                Exit Do
            Else
                ActiveCell.Offset(1, 0).Select
            End If
        Loop
        ActiveCell.Offset(1, 0).Select
    Loop
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,440
Office Version
365
Platform
Windows
How about
Code:
Sub srosk()
   Dim Cl As Range
   
   For Each Cl In Range("B1", Range("B" & Rows.Count).End(xlUp))
      If Application.CountIf(Range("B:B"), Cl.Value) > 1 Then
         MsgBox "Duplicate fund found, please check fund list"
         Exit Sub
      End If
   Next Cl
End Sub
 

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
Written so simply.. thank you!

How about
Code:
Sub srosk()
   Dim Cl As Range
   
   For Each Cl In Range("B1", Range("B" & Rows.Count).End(xlUp))
      If Application.CountIf(Range("B:B"), Cl.Value) > 1 Then
         MsgBox "Duplicate fund found, please check fund list"
         Exit Sub
      End If
   Next Cl
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,440
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,082,604
Messages
5,366,581
Members
400,904
Latest member
ndaines meriabi

Some videos you may like

This Week's Hot Topics

Top