+ Reply to Thread
Results 1 to 4 of 4

Stop Duplicated rows

Hybrid View

  1. #1
    Registered User
    Join Date
    10-28-2009
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    1

    Stop Duplicated rows

    Hi, im creating a Customer Details Spreadsheet. I'm Using a UserForm to Input the Customer Details. For the Street Name and Area I'm using a Combo Box. For the House Number i have a seperate column.

    What I wish to do is input data using the UserForm, but i don't want to duplicate the same house number in that street

    Example:

    I can add 123 Springfield Road and also add 123 Johnson Road [more that one with same house number] and I can also add 109 Johnson Road [more than one per street], but i dont want to be able to add 123 Springfield Road again, and when i click on submit on the UserForm it gives me a Error.

    Thankx

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Stop Duplicated rows

    1) Add an "error check" column that is basically a concatenation of your address fields.... =A1 & " " & B1
    Anytime an item is added to your database, make sure that cells gets filled in, too. Let's say that column is C.

    2) Do an IF test at the start of your SUBMIT routine. Something like:
     If WorksheetFunction.Countif(Range("C:C"),TextBox1.Value & " " & TextBox2.Value) > 0 Then
       MsgBox "Address exists in database already"
       Exit Sub
    End If
    
    ....the rest of the macro code
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Stop Duplicated rows

    Another option: Use the Find method to see if the Street Name exists and whether or not the house number is different.

    Basic Code:
    Private Sub CommandButton1_Click()
    
        Dim strStreet As String, Msg As String
        Dim rFound As Range, rRng As Range
        Dim lrow As Long, lHousenum As Long
    
    
        lrow = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
        lHousenum = Me.TextBox1.Value
        Set rRng = Sheet1.Range("B2:B" & lrow) 'where to search for the value
        strStreet = Me.TextBox2.Text
    
        Msg = "The house number and street already exists - duplicate entries not permitted."
    
        Set rFound = rRng.Find(strStreet, LookIn:=xlValues)
        If Not rFound Is Nothing Then
            Select Case rFound.Offset(0, -1).Value
                Case Is = lHousenum
                    MsgBox (Msg), vbExclamation
                    Me.TextBox1.Value = vbNullString
                    Me.TextBox2.Value = vbNullString
                Case Else
                    'code to write to sheet
            End Select
        End If
    
    End Sub
    Attached Files Attached Files
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Stop Duplicated rows

    That won't find all the matches. If the address to search for is 123 Main St and the first 123 it finds is 123 Lemon Lane, it would mark that as not a match, when in fact 123 Main St may exist elsewhere.

+ 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