+ Reply to Thread
Results 1 to 6 of 6

Help finding and tracking unique duplicate values, append # to name

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2012
    Location
    NoVA
    MS-Off Ver
    2010
    Posts
    3

    Help finding and tracking unique duplicate values, append # to name

    I have a dynamic 1D range (up to 15 rows) to search for unique duplicate values. All appear in Column B. Instead of removing duplicate values, I want to append a number to the end of the value like Windows does for file names.

    Name, Another Name, Some Name, Name (2), YourMomsName, Some Name (2), Al Bundy, Name (3)

    I have a macro to find the range of the names, and I've been fiddling with macros to find duplicate values. What I need help with is tracking the unique ones, ignoring the first instances, then updating the name with the correct number added to it.

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Help finding and tracking unique duplicate values, append # to name

    Something like:

    Public Sub AppendSeq(ByVal Target As Excel.Range)
    
        Dim scpValues           As Object
    
        Dim arrValues           As Variant
        Dim arrOutput           As Variant
    
        Dim lngRow              As Long
        Dim intOccur            As Integer
        Dim strOccur            As String
    
        Set scpValues = CreateObject("Scripting.Dictionary")
        scpValues.CompareMode = TextCompare
    
        arrValues = Target.Resize(Target.Rows.Count, 1).Value
        ReDim arrOutput(LBound(arrValues, 1) To UBound(arrValues, 1), 1 To 1)
        For lngRow = LBound(arrValues, 1) To UBound(arrValues, 1)
            strOccur = ""
            If scpValues.Exists(arrValues(lngRow, 1)) Then
                intOccur = 1
                While scpValues.Exists(arrValues(lngRow, 1) & " (" & intOccur & ")")
                    intOccur = intOccur + 1
                Wend
                scpValues(arrValues(lngRow, 1) & " (" & intOccur & ")") = lngRow
                strOccur = " (" & intOccur & ")"
            Else
                scpValues(arrValues(lngRow, 1)) = lngRow
            End If
            arrOutput(lngRow,1) = arrValues(lngRow, 1) & strOccur
        Next lngRow
    
        Target.Value = arrOutput
    
        Set scpValues = Nothing
    
    End Sub
    Just pass the range to it. The routine loads the given range values into an array, then loops through the array. The Scripting.Dictionary object is an easy way to check for duplicates, here it first checks if a value has already been added, if so, it increments a counter until it finds one that hasn't. It puts the adjusted value into an output array, then at the end places the output array back into the target values.
    Last edited by wallyeye; 09-21-2012 at 12:50 PM.

  3. #3
    Registered User
    Join Date
    09-18-2012
    Location
    NoVA
    MS-Off Ver
    2010
    Posts
    3

    Re: Help finding and tracking unique duplicate values, append # to name

    You rock. It's working nicely. I changed it ever-so slightly so duplicates would begin at "(2)." Thank you!

    ...
    scpValues(aryValues(lngRow, 1) & " (" & intOccur & ")") = lngRow
    strOccur = " (" & intOccur + 1 & ")"
    ...

  4. #4
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Help finding and tracking unique duplicate values, append # to name

    Glad to be of help.

  5. #5
    Registered User
    Join Date
    09-18-2012
    Location
    NoVA
    MS-Off Ver
    2010
    Posts
    3

    Re: Help finding and tracking unique duplicate values, append # to name

    New scope for this function. What I have the worksheet doing is allowing users to enter names and create sheets based on those names. The rows containing the names they enter on the main worksheet get locked. However, the user can still go back to the main sheet and add rows with names under the locked cells and create sheets again (bypassing locked cells of names already created). So the goal now for this duplicates function is to be able to catch previously appended names. If there's already a Name (2), and the user enters another row with Name, this function changes it to Name (2) and Excel shoots a sheet name error. I'd like it to know that Name (2), Name (3), Name (4), etc. are still duplicates of Name.

    One possible solution might be to go through the new list of names and remove any appended text before checking for duplicates, then re-insert the array of values. However, the user may enter names with parenthetical text and numbers, so it could be tricky.

  6. #6
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Help finding and tracking unique duplicate values, append # to name

    You probably should post this in a new thread, get some new blood in here...

+ 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