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.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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.
For example

Cells A1:A5 I want to prevent users from selecting a duplicate vaule.

Cells A10:A15 I want to prevent users from selecting a duplicate value.

Cells A20:A25 I want to prevent users from selecting a duplicate value.
 
Upvote 0
Unless there are duplicates in the list that is used for the Validation, they can't "Select" duplicates.
 
Upvote 0
Unless there are duplicates in the list that is used for the Validation, they can't "Select" duplicates.
There are no duplicates in the list being used for data validation. However from the dropdown list.l, I am able to select duplicate values for each cell.
 
Upvote 0
That's kinda the point of a drop down list. Do you want to create a list where if someone picks a value, that value will be subtracted from the list?
 
Upvote 0
That's kinda the point of a drop down list. Do you want to create a list where if someone picks a value, that value will be subtracted from the list?
Yes that will work as well. So for range A1:A5 subtracted from the list.

Range A10:A15 which uses the same list prevent duplicates within that range etc.
 
Upvote 0
If so, that will take some serious VBA coding. And data structure knowledge would be necessary. Maybe @Jaafar Tribak can help.
 
Upvote 0
I was thinking something less complex with just throwing an error message.
I know it can be done using the data validation but was unsure of how to input the formula since I am already utilizing the data validation for the dropdown list.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

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