+ Reply to Thread
Results 1 to 12 of 12

Prevent Duplicate Customer Names

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Prevent Duplicate Customer Names

    Hello,

    On the Schedule sheet there is option to add new customer with VBA and then option to save. How can this prevent a duplicate customer name?

    Also can the customer selection drop down be in last name alphabetical order please?

    Any help or suggestions would be appreciated.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,457

    Re: Prevent Duplicate Customer Names

    Looking at the code, you do not appear to differentiate between (Or have the ability to) a NEW contact and UPdate to an existing customer

    so perhaps you need think about this.

    For duplicates, I would simply do a "Lookup" on the customer list, using customer name.
    Last edited by JohnTopley; 09-05-2023 at 04:26 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Re: Prevent Duplicate Customer Names

    I should have changed the sheet name 'Customers' to 'Contacts'. I was trying to combine features from different workbooks.
    Last edited by billy60; 09-05-2023 at 04:34 PM.

  4. #4
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Re: Prevent Duplicate Customer Names

    I should have asked for a way to check if that customer exists in the system (Contacts sheet). I will try to figure it out sorry for the trouble.

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,457

    Re: Prevent Duplicate Customer Names

    You could use COUNTIF to check if it returned 1 which indicates the customer already exists.

  6. #6
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Re: Prevent Duplicate Customer Names

    I have no idea how to do that in the save code.

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Prevent Duplicate Customer Names

    Sub Example()
        ' Column J is where to search in this example, Mary Smith is the name.
        ' Change as needed to fit your workbook.
        If Application.WorksheetFunction.CountIf(Range("J:J"), "Mary Smith") > 0 Then
            ' Name already exists cancel out of adding it.
        Else
            ' Name is new add to workbook.
        End If
    End Sub
    Last edited by skywriter; 09-05-2023 at 09:27 PM. Reason: Fixed typo.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  8. #8
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Re: Prevent Duplicate Customer Names

    Thank you I will try in the morning.

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,457

    Re: Prevent Duplicate Customer Names

    in sheet "Schedule"

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("E13")) Is Nothing Then
        If Application.WorksheetFunction.CountIf(Sheets("Customers").Range("B:B"), Target) > 0 Then
            MsgBox " Contact " & Target & " already exits"
        Else
            ' Name is new add to workbook.
        End If
    Else
        If Not Intersect(Target, Range("E3")) Is Nothing And Range("E3").Value <> Empty Then Contact_Load 'Load Contact Details
    End If
    End Sub
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    314

    Re: Prevent Duplicate Customer Names

    Also as an option:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Contacts As Worksheet: Set Contacts = ThisWorkbook.Worksheets("Contacts")
        If Target.CountLarge > 1 Then Exit Sub
        
        If Not Intersect(Target, Range("E13")) Is Nothing Then
            
            If Application.WorksheetFunction.CountIf(Contacts.Range("B:B"), Target) > 0 Then
                Application.Goto Target
                MsgBox " Contact " & Target & " Already Exits! ", vbExclamation
            Else
                MsgBox " Contact " & Target & " is new in Contacts List", vbInformation
            End If
        
        Else
            If Not Intersect(Target, Range("E3")) Is Nothing And Range("E3").Value <> Empty Then Contact_Load    'Load Contact Details
        End If
    
        Set Contacts = Nothing
    End Sub
    Write the given code to the Schedule Sheet Module. Good Luck.

  11. #11
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    973

    Re: Prevent Duplicate Customer Names

    I tried John Topley's solution. There is an issue though. The calendar has appointments that can be selected to show the details of that appointment. When clicking the popup shows that customer exists.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,641

    Re: Prevent Duplicate Customer Names

    billy60
    Prevent Duplicate Customer Names
    Is it like that simple?
    If you are managing Appointment not to overwrap for the Customer then
    1)
    Insert bold lines in "Sub Appt_SaveUpdate"
        End If
        Dim s
        s = AppExists(Schedule.[e3], Schedule.[e5], Schedule.[e6], Schedule.[e7])
        If s <> "" Then MsgBox "Appoint overwrapping" & vbLf & s, vbCritical: Exit Sub
        Appts.Range("B" & ApptRow).Value = .Range("B12").Value 'Set Contact ID
    2) Add below to the same code module
    Function AppExists(sName, sDate, sTime, eTime) As String
        Dim x, i As Long
        With Appts.[a3].CurrentRegion
            x = Filter(Appts.Evaluate("transpose(if((" & .Columns(3).Address & "=""" & sName & """)*" & _
            "(" & .Columns(4).Address & "=" & CLng(sDate) & "),row(1:" & .Rows.Count & ")))"), False, 0)
            If UBound(x) = -1 Then Exit Function
            For i = 0 To UBound(x)
                If ((sTime >= .Cells(x(i), 5)) * (sTime <= .Cells(x(i), 6))) + _
                    ((eTime <= .Cells(x(i), 5)) * (eTime <= .Cells(x(i), 6))) Then
                    AppExists = AppExists & vbLf & Join(Array(.Cells(x(i), 4).Text, _
                        .Cells(x(i), 5).Text, .Cells(x(i), 6).Text))
                End If
            Next
        End With
    End Function
    3)
    Also can the customer selection drop down be in last name alphabetical order please?
    Add following lines in Schedule Worksheeet_Activate event procedure.
    With [e3]
        .Validation.Delete
        .Validation.Add 3, Formula1:=Join([transpose(unique(sortby(cont_name,mid(cont_name,find(" ",cont_name)+1,len(cont_name)))))], ",")
    End With
    All above is assuming your other part of codes are all working correctly.

+ 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. Customer Number vs Customer Names
    By MarkJohn51 in forum Excel General
    Replies: 14
    Last Post: 06-10-2020, 12:25 PM
  2. [SOLVED] Harmonisation of customer names
    By RSRSRS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-20-2019, 01:05 PM
  3. vba excel - Prevent opening workbook with duplicate vba code but different file names
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-03-2018, 06:36 PM
  4. [SOLVED] Prevent Errors for Duplicate Tab Names When Naming Tabs Using Cell Reference
    By DeRo22 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2014, 03:26 AM
  5. VBA code to ensure duplicate names map to same "customer number"
    By laurenh7 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-19-2012, 03:06 AM
  6. Customer Names into Customer List
    By dustin470 in forum Excel General
    Replies: 2
    Last Post: 02-13-2010, 08:52 AM
  7. [SOLVED] Sorting customer names
    By seeking improvment in forum Excel General
    Replies: 4
    Last Post: 09-29-2005, 11:05 PM

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