Data validation - prevent duplicates

kiwikiki718

Board Regular
Joined
Apr 7, 2017
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hi I have a dropdown list I created using the data validation option based on list I have in another sheet.

I was wondering if I could also prevent users from selecting a duplicate value from the dropdown list within the same cells.
 
You can use the worksheet change event ... something along these lines:

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Const TARGET_RANGE_ADDRESS = "A1:A5,A10:A15,A20:A25"

    On Error GoTo RestoreAppEvents
    With Application
    If Not (.Intersect(Target, Range(TARGET_RANGE_ADDRESS)) Is Nothing Or IsEmpty(Target.Cells(1&))) Then
        If IsDuplicate(Range(TARGET_RANGE_ADDRESS), Target.Text) Then
            .EnableEvents = False
            .Undo
            MsgBox "Duplicates not allowed in range : " & TARGET_RANGE_ADDRESS, vbCritical
        End If
    End If
RestoreAppEvents:
    .EnableEvents = True
    End With

End Sub

Private Function IsDuplicate(ByVal Rng As Range, ByVal InputVal As String) As Boolean
    Dim i As Long, n As Long
    For i = 1& To Rng.Areas.Count
        n = n + Evaluate("=COUNTIF(" & Rng.Areas(i).Address & ",""" & InputVal & """)")
    Next i
    IsDuplicate = n > 1&
End Function
Thank you this works but I would like to treat the duplicates within the 3 ranges independent. For example user will not be able to select the same value within range A1:A5 only. Same for A10: to A15 only etc.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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