+ Reply to Thread
Results 1 to 25 of 25

Remove Duplicate strings in a cell

  1. #1
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Remove Duplicate strings in a cell

    Cell A1: 001-001, 004-006, 007-090, 004-006, 100-300, 040-001, 090-001, 500-555

    suppose we have the content in cell A1.

    I have a working function from this forum that works (i.e. remove duplicates)

    Now, I have need to modify the IF statement to NOT add the "040-001" and the the likes (090-001), but I need to keep the "001-001". I know 001-001 is one variant of ###-001. Would be great if someone with more brain power to help me out with this logic. Thanks!

    ...
    ...
    For Each x In Split(txt, delim)
    if Trim(x) <> "" AND Not .exists(Trim(x)) AND Not (Trim(x) Like "###-001") Then .add Trim(x), Nothing 'This works, but it also don't add the 001-001 record
    Next
    ...
    ...

    Furthermore, I would like to develop a logic where the not to pick up any string that the first 3#s is greater than the 3#s after the dash. e.g. 100-101 => add, 100-001 => do not add to final list
    I know this will involve converting the string to number with 3 digits and compare the first 3-digit number to the 2nd 3-digit number.

    Thanks in advance.
    Last edited by beijing0305; 03-31-2021 at 01:30 PM.

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Remove Duplicate strings in a cell

    so, perhaps you could try:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by XLent View Post
    so, perhaps you could try:

    Please Login or Register  to view this content.
    Got a #VALUE!, but if I put parenthesis between your suggested conditions, I got a null in return. Could you tell me what Evaluate here are supposed to have returned? Trim(x) = "001-001" would return True. Since "001-001" is part of "###-001", Not (Trim(x) Like "###-001") would be False.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Remove Duplicate strings in a cell


    According to your original cell A1 content, what could be the expected result ? Well elaborate each step …

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,905

    Re: Remove Duplicate strings in a cell

    You could use a collection to remove the duplicates an then check for the other parameters.
    Input 001-001, 004-006, 007-090, 004-006, 100-300, 040-001, 090-001, 500-555
    Output 001-001, 004-006, 007-090, 100-300, 500-555

    Please Login or Register  to view this content.
    Last edited by ByteMarks; 03-31-2021 at 06:27 PM.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this …


    A VBA array demonstration as a beginner starter alternative :

    PHP Code: 
    Sub Demo1()
      Const 
    ", "
        
    Dim S$(), U$(), VW$(), L&
            
    Split([A1].TextD)
            
    ReDim U(1 To UBound(S) + 1)
        For 
    Each V In S
            
    If IsError(Application.Match(VU0)) Then
                W 
    Split(V"-")
                If 
    W(0) <= W(1Then L 1U(L) = V
            End 
    If
        
    Next
            
    If L Then ReDim Preserve U(1 To L) Else Split("")
            
    Debug.Print Join(UD)
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 03-31-2021 at 10:33 PM. Reason: optimization …

  7. #7
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,905

    Re: Remove Duplicate strings in a cell

    Even better!

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Thanks ByteMarks.

    And for duplicate strings within the source cell it could be easily achieved
    with an Excel basics like the MATCH worksheet function just using another array variable …

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Remove Duplicate strings in a cell


    Post #6 edited in order to remove any duplicate string …

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Try this …

    Quote Originally Posted by Marc L View Post
    PHP Code: 
       If "001-001" Or Not V Like "*-001" Then 
    I am not sure you can count on "-001" being the only value that needs to be removed. The OP said "I would like to develop a logic where the not to pick up any string that the first 3#s is greater than the 3#s after the dash". I can imagine lots of scenarios where this condition would be met and the last 3 digits are not "001". Here is my interpretation of the solution I think the OP is looking for...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 03-31-2021 at 08:57 PM.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Hi Rick,

    you are right, when it seems too easy the glitch may come, thanks ! Post #6 code updated.

    Your demonstration working with a String variable is good enough for short string.
    I thought about the same but as the initial post is not well elaborated, as it seems the OP uses something like a Collection
    like ByteMarks did in his post #5 so I wanna show the array alternative …
    Last edited by Marc L; 03-31-2021 at 10:39 PM. Reason: typo …

  12. #12
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Remove Duplicate strings in a cell

    Obviously there have been subsequent follow ups / suggestions, but in reference to post#3

    Quote Originally Posted by beijing0305 View Post
    Got a #VALUE!,
    without the full code it's hard to debug, but the suggested code works ok for me using a basic Dictionary setup of:
    Please Login or Register  to view this content.
    the revised line has an AND/OR logical approach for establishing valid values, more specifically:

    AND -- not blank, unique, not like ###-001, and first value <= second value
    OR -- value is 001-001

    Could you tell me what Evaluate here are supposed to have returned?
    Evaluate will satisfy below criteria:
    ...not to pick up any string that the first 3#s is greater than the 3#s after the dash
    i.e. Evaluate will execute the Trim(x) as a formula, e.g. 100-101 -> -1

    so, where A1 holds 001-001, 004-006, 007-090, 004-006, 100-300, 040-001, 090-001, 500-555

    B1: =UniqueItems(A1) --> 001-001, 004-006, 007-090, 100-300, 500-555

  13. #13
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Remove Duplicate strings in a cell

    Try this

    Please Login or Register  to view this content.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  14. #14
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by ByteMarks View Post
    You could use a collection to remove the duplicates an then check for the other parameters.
    Input 001-001, 004-006, 007-090, 004-006, 100-300, 040-001, 090-001, 500-555
    Output 001-001, 004-006, 007-090, 100-300, 500-555

    Please Login or Register  to view this content.
    Thank you very much for the response. This is exactly the output I am expecting to see. Didn't realize I could convert string to integer using Int function. I can also follow the code for the most part. A couple of questions. Could you please help me understand your code better?

    (1) cln.Add y, y: This adds the y to the collection cln, what's the second parameter "y" do?
    (2) ReDim result(myCol.Count - 1): I assume this shortens the array by 1, but why is always going to be one less?

    Thanks again for the solution. Now, I need to learn/understand this for future application.

  15. #15
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Try this …

    Quote Originally Posted by Marc L View Post

    A VBA array demonstration as a beginner starter alternative :

    PHP Code: 
    Sub Demo1()
      Const 
    ", "
        
    Dim S$(), U$(), VW$(), L&
            
    Split([A1].TextD)
            
    ReDim U(1 To UBound(S) + 1)
        For 
    Each V In S
            
    If IsError(Application.Match(VU0)) Then
                W 
    Split(V"-")
                If 
    W(0) <= W(1Then L 1U(L) = V
            End 
    If
        
    Next
            
    If L Then ReDim Preserve U(1 To L) Else Split("")
            
    Debug.Print Join(UD)
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    This works as well, but I don't know why. ::lack brain power:: Appreciate if you could please put some comment/logic to help me follow your code. I will study up the Dim *$() *& suppose to be, and ReDim Preserve. Looks like working with arrays...Thanks!

  16. #16
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Remove Duplicate strings in a cell

    I know the code I posted in Message #10 is short, but I am almost certain that it works correctly. Maybe try it and see?

  17. #17
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Remove Duplicate strings in a cell

    Rick, I agree -- #6, #10, and #12 will all generate the same results (bar a space or two), but I prefer your approach, given stated requirement.

  18. #18
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,905

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by beijing0305 View Post
    Thank you very much for the response. This is exactly the output I am expecting to see. Didn't realize I could convert string to integer using Int function. I can also follow the code for the most part. A couple of questions. Could you please help me understand your code better?

    (1) cln.Add y, y: This adds the y to the collection cln, what's the second parameter "y" do?
    (2) ReDim result(myCol.Count - 1): I assume this shortens the array by 1, but why is always going to be one less?

    Thanks again for the solution. Now, I need to learn/understand this for future application.
    Hi

    cln.Add(y,y) is adding y to both the Item and Key properties. The Key property has to be unique which is why it works for removing duplicates. It's also why it's preceded by "On Error Resume Next" so that it doesn't throw an error if an attempt is made to add a duplicate key.


    ReDim result(myCol.Count - 1)
    The reason is that arrays are usually zero based, i.e. start at 0. Collections on the other hand start at 1
    So if the collection had 10 items it'd be indexed 1-10, but the array would be 0-9. myCol.count would be 10, but the upper bound of the array would be 9, hence the -1


    ReDim stands for ReDimension, so it's sizing the array to the collection. The reason for that ColToArray function is to convert the collection to an array so we can use Join to put the string back together.
    Last edited by ByteMarks; 04-01-2021 at 03:54 PM.

  19. #19
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,905

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by Rick Rothstein View Post
    I know the code I posted in Message #10 is short, but I am almost certain that it works correctly. Maybe try it and see?
    Nice compact solution. The only thing is it doesn't seem to put the original delimiter back in and if you put it in after the Then statement you end up with a leading comma?

  20. #20
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by ByteMarks View Post
    Nice compact solution. The only thing is it doesn't seem to put the original delimiter back in and if you put it in after the Then statement you end up with a leading comma?
    Whoops! I forgot about that. Change the last line of code to this...
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by kvsrinivasamurthy View Post
    Try this

    Please Login or Register  to view this content.
    Worked perfectly! Thank you for sharing your brain power with me. Trying to break down the logic, please correct me if I miss stating...Thanks for such an elegant solution.

    If Trim(x) <> "" AND _ ' if not empty
    Not .exists(Trim(x)) AND _ ' if exists
    (Not (Trim(x) Like "###-001") Or _ 'first pattern
    Trim(x) Like "001-001") Or _ '2nd pattern
    Val(Left(Trim(x), 3)) < Val(Right(Trim(x), 3)) _ '3 condition when 1st set of ### is larger than 2nd set of ###...If I have Val(Left(Trim(x), 3)) <= Val(Right(Trim(x), 3), I could eliminate the first 2 patterns above?
    ) Then, .Add Trim(x), Nothing
    Last edited by beijing0305; 04-01-2021 at 08:02 PM.

  22. #22
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by ByteMarks View Post
    Hi

    cln.Add(y,y) is adding y to both the Item and Key properties. The Key property has to be unique which is why it works for removing duplicates. It's also why it's preceded by "On Error Resume Next" so that it doesn't throw an error if an attempt is made to add a duplicate key.


    ReDim result(myCol.Count - 1)
    The reason is that arrays are usually zero based, i.e. start at 0. Collections on the other hand start at 1
    So if the collection had 10 items it'd be indexed 1-10, but the array would be 0-9. myCol.count would be 10, but the upper bound of the array would be 9, hence the -1


    ReDim stands for ReDimension, so it's sizing the array to the collection. The reason for that ColToArray function is to convert the collection to an array so we can use Join to put the string back together.
    Thank you very much for the detailed explanation.

  23. #23
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    I did get an extra space in the result. Besides that, the solution is identity. Thanks.

  24. #24
    Registered User
    Join Date
    02-12-2021
    Location
    America
    MS-Off Ver
    2016
    Posts
    38

    Re: Remove Duplicate strings in a cell

    Quote Originally Posted by Rick Rothstein View Post
    I know the code I posted in Message #10 is short, but I am almost certain that it works correctly. Maybe try it and see?
    All worked!

  25. #25
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Remove Duplicate strings in a cell

    Thanks for feed back and rep.

+ 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. Remove duplicates from a string only from right
    By maamon in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-13-2021, 09:42 PM
  2. [SOLVED] Remove Duplicates Within a Cell
    By soleimani1967 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-07-2019, 02:14 PM
  3. Replies: 6
    Last Post: 11-27-2018, 07:17 AM
  4. [SOLVED] Remove duplicates in Excel 2016 text string in one cell
    By martin7b in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-31-2017, 02:07 AM
  5. VBA: remove duplicates from cell or string
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-22-2017, 07:58 AM
  6. [SOLVED] Concatinate string of text and remove duplicates
    By msmayhugh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-20-2015, 06:38 PM
  7. remove duplicates from csv string
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2014, 01:24 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