Results 1 to 6 of 6

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

Threaded View

deqx Help finding and tracking... 09-21-2012, 09:06 AM
wallyeye Re: Help finding and tracking... 09-21-2012, 12:45 PM
deqx Re: Help finding and tracking... 09-23-2012, 01:14 AM
wallyeye Re: Help finding and tracking... 09-24-2012, 10:55 AM
deqx Re: Help finding and tracking... 10-01-2012, 09:24 PM
wallyeye Re: Help finding and tracking... 10-02-2012, 10:46 AM
  1. #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.

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