+ Reply to Thread
Results 1 to 9 of 9

how to remove duplicates from an array

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    how to remove duplicates from an array

    so i have the following code which creates an array of "accounts" from 3 workbooks. now i want to remove duplicates from the array. i have searched the net for common code to remove the duplicates but the problem is that it doesn't do anything to the array (at least as far as i can see). so here is the code that works (in that it creates an array).

    Please Login or Register  to view this content.
    so here is code from wellsr.com

    Please Login or Register  to view this content.
    so if i understand the function, i should be able to do the following in the original code above:

    Please Login or Register  to view this content.
    so the code should print the upper bound of the array (before deduping)
    then remove the duplicates (there are many)
    then show the new upper bound of the array (less than the first ubound)

    since i have tried many routines to remove the duplicates, i presume that i am missing something. What is it?

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: how to remove duplicates from an array

    Why not just put the values straight into the dictionary, rather than using an array?

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: how to remove duplicates from an array

    You need to put the result back into variable.

    Ex:
    Please Login or Register  to view this content.
    Function does not alter the original array, but creates new array using keys of dictionary object.

    Personally, I prefer to first load Range.Value into variant array. Then process it as needed. Instead of iterating over range to fill array.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to remove duplicates from an array

    so thanks CK and Fluff.

    fluff - im not so proficient with dictionaries, might want some help. can you modify my code above to do just that?

    ck - so lets say my array is such arr(1)=1, arr(2) = 1, arr(3) = 2, arr(4) = 3, arr(5) = 2, will it remove all the 1's and 2's or leave 1 of each? when i ran what you posted, my array was much less that what it should be. does the array have to be sorted first?

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: how to remove duplicates from an array

    How about
    Please Login or Register  to view this content.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: how to remove duplicates from an array

    Implicitly adding item to dictionary will keep 1 of each. As key must be unique.
    I.E. If it does not exist in dictionary, it adds key and item pair. If it does exist, it updates item of that key.

    Ex:
    Please Login or Register  to view this content.
    First debug should return 4 (as it's 0 base array) and 2nd returns 2 (1, 2, 3 = 3 items, index of 0 to 2).

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

    Re: how to remove duplicates from an array

    Instead of creating an array with duplicates just avoid to add a duplicate within the array !

    Just use Application.Match for example, it returns an error (check with IsError) if a value does not exist in an array …

  8. #8
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to remove duplicates from an array

    thanks ck and marc, i will investigate your solutions.

    fluff,

    when i tried to add using dic.add item, it didn't return the item after adding it. don't know why, but your solution works. thanks

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: how to remove duplicates from an array

    You're welcome & thanks for the feedback

+ 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. haw to remove duplicates from an array
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-18-2019, 01:39 PM
  2. Replies: 9
    Last Post: 01-18-2018, 03:00 AM
  3. [SOLVED] Remove Duplicates Column Array Value From Cell
    By yoshi_5 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-25-2016, 02:18 AM
  4. Remove duplicates from array and count number of values
    By JDG94 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2016, 02:02 PM
  5. [SOLVED] Remove duplicates and sort array
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 35
    Last Post: 10-11-2012, 10:22 AM
  6. Remove duplicates from 1 dimensional array in VBA
    By zaphodb2003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-08-2010, 04:18 AM
  7. Remove duplicates from one dimensional array
    By asha3010 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-29-2010, 05:09 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