Results 1 to 14 of 14

Thread: Macro Question - Find duplicate and make unique

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Macro Question - Find duplicate and make unique

    Hi,

    I'm not sure if this is possible but I need to make a macro that searches a column for duplicates. If it finds a duplicate I need to it to make it unique by adding a .1 at the end. I would also want it to continue to add unique identifiers if there are say 5 duplicates of the same field.

    For example it would find 5185 in the column five times. It would then proceed to change the duplicates to 5185.1 - 5185.2 - 5186.3 - 5185.4 - 5185.5

    Is this possible to do?

    Thanks!

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Macro Question - Find duplicate and make unique

    Is the source column always going to start out with integers? If so, it will be easier. There are several approaches to searching the column. Is it acceptable if the uniquely tagged cells are formatted as text? Or do they need to be floating point numbers? Will the number of unique tags ever roll over to "10"? If so, you'll have a problem with leading and trailing zeros if you don't make them text strings. Also, sorting could be problematic depending on the format.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Macro Question - Find duplicate and make unique

    Is the source column always going to start out with integers?
    Yes the column will always be numbers. They are record numbers so they range from single digits up to five digits in length.

    Is it acceptable if the uniquely tagged cells are formatted as text? Or do they need to be floating point numbers?
    Formatting them as text will not be a problem.

    Will the number of unique tags ever roll over to "10"?
    It is possible yes.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Macro Question - Find duplicate and make unique

    VB Code:
    1. Public Sub MarkDups()
    2.     Dim tmp As String
    3.     Dim cntr As Integer
    4.    
    5.     For x = 1 To 30 'rows in my test
    6.         tmp = Range("A" & x).Text
    7.         cntr = 1
    8.         For y = 1 To 30
    9.             If y <> x Then
    10.                 If tmp = Range("A" & y).Text Then
    11.                     Range("A" & y) = Range("A" & y) & "." & cntr
    12.                     cntr = cntr + 1
    13.                 End If
    14.             End If
    15.         Next
    16.     Next
    17. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Macro Question - Find duplicate and make unique

    That works great thanks!

    One other question. Is it possible for the code to know when the column is done and stop looking for duplicates. I need something more dynamic so that there isn’t any clean up needed afterword.

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Macro Question - Find duplicate and make unique

    That may work great for 30 items, but try it for 50,000! It may take awhile! Each step of X eliminates that row as a possible duplicate, so I think you can change the following line:
    Code:
    For y = 1 To 30
    'to
    For y = x to 30
    That way the inner loop won't re-scan items that are already eliminated as duplicates.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Macro Question - Find duplicate and make unique

    make that:
    Code:
    For y = x + 1 To 30
    and eliminate the test "If y <> x".
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8

    Re: Macro Question - Find duplicate and make unique

    The code which static wrote, didnt produce the right output...I would rather prefer the following code :

    VB Code:
    1. Public Sub MarkDups()
    2.     Dim tmp As String
    3.     Dim cntr As Integer
    4.    
    5.     For x = 1 To 30 'rows in my test
    6.         tmp = Range("A" & x).Text
    7.         cntr = 1
    8.                    
    9.        
    10.         For y = x + 1 To 30
    11.                            
    12.                 If tmp = Range("A" & y).Text Then
    13.                         If cntr Mod 10 = 0 Then
    14.                         cntr = cntr + 1
    15.                         End If
    16.                     Range("A" & y) = Range("A" & y) & "." & cntr
    17.                     cntr = cntr + 1
    18.                 End If
    19.         Next
    20.     Next
    21. End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Macro Question - Find duplicate and make unique

    Is there a way for it to check if the cell is blank and if it is then stop or not do anything?

  10. #10

    Re: Macro Question - Find duplicate and make unique

    Check if you want this :

    VB Code:
    1. Public Sub MarkDups()
    2.     Dim tmp As String
    3.     Dim cntr As Integer
    4.    
    5.     For x = 1 To 30 'rows in my test
    6.         tmp = Range("A" & x).Text
    7.         cntr = 1
    8.                    
    9.         If tmp = "" Then
    10.         goto GoOut
    11.         End If
    12.        
    13.         For y = x + 1 To 30
    14.                            
    15.                 If tmp = Range("A" & y).Text Then
    16.                         If cntr Mod 10 = 0 Then
    17.                         cntr = cntr + 1
    18.                         End If
    19.                     Range("A" & y) = Range("A" & y) & "." & cntr
    20.                     cntr = cntr + 1
    21.                 End If
    22.         Next
    23.     Next
    24.  
    25. GoOut:
    26.  
    27. End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Macro Question - Find duplicate and make unique

    Quote Originally Posted by vikasbhandari2
    Check if you want this :

    VB Code:
    1. Public Sub MarkDups()
    2.     Dim tmp As String
    3.     Dim cntr As Integer
    4.    
    5.     For x = 1 To 30 'rows in my test
    6.         tmp = Range("A" & x).Text
    7.         cntr = 1
    8.                    
    9.         If tmp = "" Then
    10.         goto GoOut
    11.         End If
    12.        
    13.         For y = x + 1 To 30
    14.                            
    15.                 If tmp = Range("A" & y).Text Then
    16.                         If cntr Mod 10 = 0 Then
    17.                         cntr = cntr + 1
    18.                         End If
    19.                     Range("A" & y) = Range("A" & y) & "." & cntr
    20.                     cntr = cntr + 1
    21.                 End If
    22.         Next
    23.     Next
    24.  
    25. GoOut:
    26.  
    27. End Sub

    Compile error sub or function not defined. It doesn't like the GoOut

  12. #12

    Re: Macro Question - Find duplicate and make unique

    Hmmmm....well Its working fine for me....please make sure that you are using the following code correctly :
    VB Code:
    1. If tmp = "" Then
    2.         GoTo GoOut
    3.         End If

  13. #13

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Macro Question - Find duplicate and make unique

    Thank you for clearing that up.
    Now here’s the next issue. That works perfectly if the column starts w/o any spaces. But mine has 3-4 blank cells in the column along with a header which is a total of about 5 cells from the beginning of the column to when the data I'm interested in marking starts.


    example:

    blank cell
    blank cell
    Record Number
    Blank cell
    blank cell
    5110
    3506
    3985
    4949

  14. #14

    Re: Macro Question - Find duplicate and make unique

    if your numbers are starting from the 6th row, then you can use the following code
    VB Code:
    1. For x = 6 To 30 'rows in my test
    Hope it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width