Unique value in a cell

Rozallyn

New Member
Joined
Jan 23, 2019
Messages
6
Hi,

how do I ensure within a range of cells in a worksheet only a unique value can be entered even when pasting into the cell. The data validation function works well for this if the data is typed in but not pasted. Is there a VBA function that I can use?
your help will be greatly appreciated
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,050
Office Version
  1. 2016
Platform
  1. Windows
Do you mean prevent duplicates being entered in a certain range ?
 
Last edited:

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,050
Office Version
  1. 2016
Platform
  1. Windows
To prevent duplicates in column A :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, r As Range, c As Range
Set rng = [A:A]
Set r = Intersect(rng, Target)
If Not r Is Nothing Then
    For Each c In r
        If WorksheetFunction.CountIf(rng, c) > 1 Then
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
            Exit For
        End If
    Next
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,436
Messages
5,528,749
Members
409,834
Latest member
vexceled

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top