+ Reply to Thread
Results 1 to 7 of 7

VBA: remove duplicates from cell or string

  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    VBA: remove duplicates from cell or string

    Hi all,

    I've got a bit of a 'simple problem, simple solution' black-out here, so posting the question here

    I've got a cell that contains something along the lines of:
    Pouch:Delicious:Fresh:Selection:Jelly:Cat Food:Dry:Happy Cat

    Now I loop through a number of strings, some of which contain duplicates of words that are already in the cell, for example, one string might be:
    Pouch:Tasty:Fresh:Selection:Jelly:Cat Food:Dry:Happy Cat:Bag
    While another might be:
    Bag:Happy Cat:Fresh:Organic:Selection:Wet:Cat Food:Dry

    While looping through my strings in VBA, I'd want to end up with only the unique words in the cell, so ultimately:
    Pouch:Delicious:Fresh:Selection:Jelly:Cat Food:Dry:Happy Cat:Tasty:Bag:Organic:Wet

    Assume the strings are one string with the colons included. I could split and loop through the splitted results, but would prefer to replace / remove any duplicates and add what's left to the cell.

    What would be the most time-efficient way to do this?
    Thanks!

    Jasper

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA: remove duplicates from cell or string

    Jasper

    You could use a dictionary for this.

    What range are you working with and where would you want the results?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: VBA: remove duplicates from cell or string

    I was thinking of a dictionary - but how would I go about this.
    Essentially I'm parsing a (large) JSON dataset, so no range - all from strings in VBA.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA: remove duplicates from cell or string

    How are you currently storing the strings?

    Where would you want the result(s) of the de-duplication to go?

  5. #5
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: VBA: remove duplicates from cell or string

    I am iterating through a JSON object through hundreds of keys, each of the keys has a string as exampled.
    I am able to identify the string, so could say "dictionary key (identifier) = value", but my problem would be - how would i add the unique values to the value when I encounter the same key?

    So let's say, I add dictionary "Jasper", with value "Pouch:Delicious:Fresh:Selection:Jelly:Cat Food:Dry:Happy Cat", how would I add just the "Tasty" and "Bag" to that value when I encounter "Pouch:Tasty:Fresh:Selection:Jelly:Cat Food:Dry:Happy Cat:Bag" (as that's one string / json key value).

    Thanks!

    Jasper

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA: remove duplicates from cell or string

    I'd use the split function to create an array for each , then loop through the elements and if the element is not a key in the dictionary, then add it. That should be relatively fast, particularly since you can use .Exists instead of having to loop through the dictionary keys. In the end you will have a dictionary of only unique strings, then loop through them to build your final output string.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA: remove duplicates from cell or string

    Would this be something you could use?

    Please Login or Register  to view this content.

+ 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. Vba to highlight the cell and remove duplicates
    By ashup in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2017, 05:21 AM
  2. [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
  3. [SOLVED] Remove duplicates from single cell
    By Petros Georgilas in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-19-2014, 02:58 PM
  4. Remove Duplicates in a Cell
    By brianmcgee in forum Excel General
    Replies: 13
    Last Post: 12-08-2014, 03:17 PM
  5. remove duplicates from csv string
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2014, 01:24 PM
  6. [SOLVED] Remove duplicates in a single cell
    By SonGoku in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-27-2013, 06:35 AM
  7. Remove duplicates within a cell
    By forzaboro in forum Excel General
    Replies: 16
    Last Post: 08-29-2011, 10:14 AM

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