Results 1 to 13 of 13

Prevent User Enter Duplicates in Column

Threaded View

  1. #1
    Registered User
    Join Date
    03-19-2011
    Location
    California, US
    MS-Off Ver
    Excel 2003
    Posts
    4

    Prevent User Enter Duplicates in Column

    I have a spreadsheet that users need to enter part numbers that we have quoted for. I need to prevent users from entering duplicates in column C. Data validation isn't an option since users copy & paste sometimes into that column.

    The code works fine if user happens to copy & paste a part number already on that list. But for some reason, sometimes the code will work & sometimes it fails to catch duplicates when users type in the part numbers. Sometimes users will insert a row & type a part number & the code won't catch the duplicate part number.

    Need help with the code to catch any duplicates in column C & sometimes the data in column C is non contiguous so the code has to check if the data entered matches any cell in that column. I would also like for the message box to also display the cell location were the original data is located.

    Below is the code used & I also attached a sample of my spreadsheet.

    Any help is greatly appreciated!

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim r As Range
    Dim ans As String
    
    Const myCol As Long = 3
    
    If Intersect(Target, Columns(myCol)) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    
    For Each r In Intersect(Target, Columns(myCol))
       If Application.CountIf(Columns(myCol), r.Value) > 1 Then
       MsgBox (r.Value & " already exists")
           r.ClearContents
       End If
    Next
    
    Application.EnableEvents = True
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1