+ Reply to Thread
Results 1 to 4 of 4

How to add suffix to duplicates to make them unique

Hybrid View

  1. #1
    Registered User
    Join Date
    11-21-2014
    Location
    Brooklyn, NY
    MS-Off Ver
    2007
    Posts
    15

    How to add suffix to duplicates to make them unique

    Hi community,

    I have a huge dataset of records each of which is supposed to have its own unique ID. But some rows improperly have the same ID. For example:

    Lisa 9045
    Sara 9046
    Dan 9046
    Tim 9046
    Jack 9047

    How do I write a macro that tells Excel to find the duplicates and then add a suffix to them so that they become unique, so that they look like this?

    Lisa 9045
    Sara 9046-1
    Dan 9046-2
    Tim 9046-3
    Jack 9047

    Thanks.
    Last edited by rominjn; 07-09-2015 at 04:30 PM.

  2. #2
    Registered User
    Join Date
    06-16-2015
    Location
    Axel, Netherlands
    MS-Off Ver
    2010, Dutch
    Posts
    2

    Re: How to add suffix to duplicates to make them unique

    Something like this?

    Currently have user select the range they want to check for doubles, you might want to put the relevant range in the program itself. Option to just select everything on the sheet is shown as remark, (I used it for a quick test,) but this would also put numbers behind your names and other duplicate data.

    Looping like this might not be the fastest option.

    Public Sub check_doubles()
    Dim counter As Integer
    Dim base As String
    Dim SearchRange As Range
    Dim SearchCell As Range
    Dim CompareCell As Range
    
    'Set SearchRange = ActiveSheet.Cells.SpecialCells(xlCellTypeConstants)
    Set SearchRange = Application.InputBox("Select range to be searched", "choose range", Type:=8)
    On Error Resume Next
    Set SearchRange = SearchRange.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    
    For Each SearchCell In SearchRange
        If SearchCell.Value <> "" And Not InStr(1, SearchCell.Value, "-") Then
            counter = 1
            base = SearchCell.Value
            For Each CompareCell In SearchRange
                If Not CompareCell.Address = SearchCell.Address And _
                CompareCell.Value = base Then
                    If counter = 1 Then
                        SearchCell.Value = base & "-" & 1
                        CompareCell.Value = base & "-" & 2
                        counter = 3
                    Else
                        CompareCell.Value = base & "-" & counter
                        counter = counter + 1
                    End If
                End If
            Next CompareCell
        End If
    Next SearchCell
    
    SearchRange.Select
    
    End Sub

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,053

    Re: How to add suffix to duplicates to make them unique

    You could do this without a macro, using a helper column, and this formula, copied down...
    =B2&IF(COUNTIF($B$2:$B$6,B2)>1,+" - "&COUNTIF($B$2:B2,B2),"")
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    11-21-2014
    Location
    Brooklyn, NY
    MS-Off Ver
    2007
    Posts
    15

    Re: How to add suffix to duplicates to make them unique

    The issue was urgent so I tried the in-cell formula that FDibbins provided and it worked great. When I have more time, I will try the VBA that Ohma suggested. Thanks to both of you!

+ 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. [SOLVED] Count duplicates and duplicates with suffix as one instance
    By ruraljur0r in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-24-2014, 12:38 AM
  2. [SOLVED] To find duplicates in a column and suffix them with numbers to make them unique
    By JishnuSurendran in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-23-2014, 06:34 AM
  3. [SOLVED] Changing a positive or negative value based on the input's suffix, then removing suffix
    By Theredwind in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-06-2014, 05:19 AM
  4. Replies: 1
    Last Post: 04-19-2013, 02:25 AM
  5. [SOLVED] adding unique number suffix
    By hmm321 in forum Excel General
    Replies: 2
    Last Post: 07-01-2012, 05:25 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