+ Reply to Thread
Results 1 to 7 of 7

Unique Values

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23

    Unique Values

    Hey guys and gals,

    I need an error box to come up when a value is the same as a value that is already entered.

    Column D has Names lets say Bill, Bob, Jack
    Column J has Names lets say Jerry, Shelly, Joe

    Now if I enter Bill into either column again I want an error box come up and tell let me know that value already exists and I want it to simply clear that cell.

    I have adapted this code, but it only works with one column how do I make it work with the other column.

    I don't want it to check the columns in between.

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    
        Dim LLoop As Integer
        Dim LTestLoop As Integer
        
        Dim Lrows As Integer
        Dim LRange As String
        Dim LChangedValue As String
        Dim LTestValue As String
        
        'Test first 200 rows in spreadsheet for uniqueness
        Lrows = 200
        LLoop = 2
        
        'Check first 200 rows in spreadsheet
        While LLoop <= Lrows
            LChangedValue = "D" & CStr(LLoop)
            
            If Not Intersect(Range(LChangedValue), Target) Is Nothing Then
                If Len(Range(LChangedValue).Value) > 0 Then
                
                    'Test each value for uniqueness
                    LTestLoop = 2
                    While LTestLoop <= Lrows
                        If LLoop <> LTestLoop Then
                            LTestValue = "D" & CStr(LTestLoop)
                            'Value has been duplicated in another cell
                            If Range(LChangedValue).Value = Range(LTestValue).Value Then
                                'Clear Contents
                                Range(LChangedValue).ClearContents
                                MsgBox Range(LChangedValue).Value & " already exists in cell D" & LTestLoop
                                Exit Sub
                            Else
                                Range(LChangedValue).Interior.ColorIndex = xlNone
                            End If
                                
                        End If
                        
                        LTestLoop = LTestLoop + 1
                    Wend
                    
                End If
            End If
            
            LLoop = LLoop + 1
        Wend
        
    End Sub
    Thanks
    Steven
    Last edited by merilvingian; 01-15-2009 at 12:14 PM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454
    Hi,

    Can you not do what is required with standard validation, using the Custom option and the formula

    =COUNTIF($A:$B,A1)<=1

    ?
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    03-16-2004
    Location
    UK
    MS-Off Ver
    2003
    Posts
    85
    This should work...

    Private Sub Worksheet_Change(ByVal Target As Range)
    For Each c In Range("A1:B5")
        If Target.Value <> "" And Target.Value = c.Value Then
            MsgBox "Value already entered elsewhere!"
            Target.ClearContents
        End If
    Next c
    End Sub
    I didn't look at your code but I think you should be able to incorporate it
    Phil

  4. #4
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23
    Well I thought that was going to work,

    It looked like it would work.

    But everytime I entered data into any cell within those ranges I would get the error all the time.

    You see only those two columns D and G have to be unique
    all other columns can have duplicates.

    Quote Originally Posted by incjourn View Post
    This should work...

    Private Sub Worksheet_Change(ByVal Target As Range)
    For Each c In Range("A1:B5")
        If Target.Value <> "" And Target.Value = c.Value Then
            MsgBox "Value already entered elsewhere!"
            Target.ClearContents
        End If
    Next c
    End Sub
    I didn't look at your code but I think you should be able to incorporate it
    Last edited by merilvingian; 01-15-2009 at 12:24 PM. Reason: False hope

  5. #5
    Registered User
    Join Date
    05-02-2004
    MS-Off Ver
    2007
    Posts
    43
    Why not use Validation as sweep has already suggested?

    TJ

  6. #6
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23
    I already have a validation that relates to a list of peoples names

    =CrewName

    How would I add the Other parts of the validation rule?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Change the validation type to custom and use

    =AND(ISNUMBER(MATCH(Me, CrewName, 0)), COUNTIF(D1:D10, Me) + COUNTIF(J1:J10, Me) = 1)

    ... Where Me is the address of the active cell when you enter the formula.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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