+ Reply to Thread
Results 1 to 6 of 6

Code to Validate for Unique Values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Question Code to Validate for Unique Values

    I am using the following within my spreadsheet (Excel Version 14.0.7015.1000) 32 Bit. It adds a value to the next empty cell within column C. I would appreciate if someone could show me how to modify this such that it throws a text error if the value is not unique to column C and then the offending cell is selected. I tried using the built-in data validation but it seems to be bypassed when using the code below.

    My excel vba level is currently "Dangerously Ignorant" so I'm happy to clarify anything you need to help.

    Private Sub TextBox1_Change()

    'Debug.Print "Outside " & IsActive

    'To prevent recursion
    If Not IsActive And TextBox1.Text <> "" Then
    IsActive = True
    Application.OnTime Now + TimeValue("00:00:03"), "Module2.CopyToCell"
    End If

    End Sub

    _______

    Private Sub UserForm_Initialize()
    IsActive = False
    TextBox1.SetFocus
    End Sub


    _________

    Dim IsActive As Boolean

    Sub CopyToCell()

    Dim Ws1 As Worksheet
    Dim LastCellInRng As Range

    Set Ws1 = Worksheets("Main")
    Set LastCellInRng = Ws1.Range("C" & Ws1.Cells.Rows.Count).End(xlUp)
    LastCellInRng.Offset(1, 0).Value = UserForm2.TextBox1.Text

    UserForm2.TextBox1.Text = ""
    UserForm2.TextBox1.SetFocus
    IsActive = False

    End Sub

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,288

    Re: Code to Validate for Unique Values

    Personally, I would use a commandbutton on my userform, and use its click event:

        
    Private Sub CommandButton1_Click()
        
        With Worksheets("Main")
            If Application.CountIf(.Range("C:C"), Me.TextBox1.Text) = 0 Then
                .Range("C" & .Rows.Count).End(xlUp)(2).Value = Me.TextBox1.Text
            Else
                MsgBox "That's already there...."
            End If
        End With
        
        Me.TextBox1.Text = ""
        Me.TextBox1.SetFocus
                
    End Sub
    Though you could probably use this in place of your code

    Sub CopyToCell()
    
        With Worksheets("Main")
            If Application.CountIf(.Range("C:C"), UserForm2.TextBox1.Text) = 0 Then
                .Range("C" & .Rows.Count).End(xlUp)(2).Value = UserForm2.TextBox1.Text
            End If
        End With
        
    UserForm2.TextBox1.Text = ""
    UserForm2.TextBox1.SetFocus
    IsActive = False
    
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to Validate for Unique Values

    Well, I found one other issue. Maybe its how I incorporated parts of both your codes. The message comes up if the value is a good or nonunique one. I coped the code below.

    Sub CopyToCell()

    With Worksheets("Main")
    If Application.CountIf(.Range("C:C"), UserForm2.TextBox1.Text) = 0 Then
    .Range("C" & .Rows.Count).End(xlUp)(2).Value = UserForm2.TextBox1.Text
    Else
    MsgBox "That Item Already Exists"
    End If
    End With

    UserForm2.TextBox1.Text = ""
    UserForm2.TextBox1.SetFocus
    IsActive = False

    End Sub

  4. #4
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to Validate for Unique Values

    Thank you so much for replying.

    I do have command buttons but please realize that for this particular part the entry is via barcode scanner so when I scan I just want it to update the next cell with that value.

    Your second code is working great with the addition of the else statement in your first code. I get the error message but I seem to need to click OK 3 times to clear the message box. Any ideas?

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,288

    Re: Code to Validate for Unique Values

    Try checking if the value is "", which whould get you out of the loop:

    Sub CopyToCell()
    If UserForm2.TextBox1.Text = "" Then Exit Sub
    
    With Worksheets("Main")
    If Application.CountIf(.Range("C:C"), UserForm2.TextBox1.Text) = 0 Then
    .Range("C" & .Rows.Count).End(xlUp)(2).Value = UserForm2.TextBox1.Text
    Else
    MsgBox "That Item Already Exists"
    End If
    End With
    
    UserForm2.TextBox1.Text = ""
    UserForm2.TextBox1.SetFocus
    IsActive = False
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to Validate for Unique Values

    That took care of it. Thank you so much for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Excel VBA Code Instead of SumIf Array Formula for Unique Values
    By excelforumkeys in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-12-2014, 03:39 PM
  2. How to adapt this code to copy unique values in cells
    By RayJay01 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2014, 07:18 AM
  3. [SOLVED] Macro that merges duplicate rows based on unique values - Need to edit current code
    By niya429 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2014, 02:57 PM
  4. Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  5. How to validate data entries to be unique within an array
    By Dwight at Boeing in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-15-2005, 06:45 PM

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