+ Reply to Thread
Results 1 to 3 of 3

Code to prevent userform from saving duplicates based on 3 textboxes

  1. #1
    Registered User
    Join Date
    09-24-2003
    Posts
    62

    Code to prevent userform from saving duplicates based on 3 textboxes

    I have a userform that users insert customer names and info and by clicking the save button it takes the values from textboxes and populates cells of a worksheet. The problem is the users are creating duplicate customers. What I would like to do is before saving the worksheet compare textbox 1, 2 and 3 to columns A, B and C of a worksheet before it adds the info.

    I thought I could use the set range .find code with the if not range is nothing then. but that only compares the first .find.

    userform.textbox1 = last name which goes into worksheet("customers").column(1)
    userform.textbox2 = first name which goes into worksheet("customers").column(2)
    userform.textbox3 = middle name which goes into worksheet("customers".column(3)

    I figured the code below would work, but doesn't

    Please Login or Register  to view this content.
    What if a customer has the same first and last name with a different middle intial?
    I'm thinking I have to loop, but not sure how. Please help.

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Code to prevent userform from saving duplicates based on 3 textboxes

    A number of possibilities:
    - introduce a (hidden) helper column holding the concatenate of columns A, B and C - and use a find on that column with a concatenated value of textboxes 1 to 3;
    - loop through all rows on the worksheet and compare columns A, B and C with Textbox values 1, 2 and 3
    - use autofilter to filter the three columns using the values from the textboxes (hide when value = equal) - if all rows are hidden it is a duplicate value
    - more options exist but they are more complex
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    09-24-2003
    Posts
    62

    Re: Code to prevent userform from saving duplicates based on 3 textboxes

    looping through all the rows and comparing columns A, B and C with textbox values of 1, 2 and 3 is what I had in mind.

    I know how to loop through a column to find and compare 1 value with 1 cell, but multiples is what I am having a problem with.

+ Reply to Thread

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