+ Reply to Thread
Results 1 to 9 of 9

Is there a quick way to compare collections in vba?

  1. #1
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Cool Is there a quick way to compare collections in vba?

    Hi guys,

    I am generating two lists of collections. I am wondering if there is a way to quickly determine if both collections are the same? Is there an inbuilt function?

    Otherwise I suppose I will have to create a few checks myself, if that is the case, then I'd probably do the following, in the following order.
    1/ check both are the same size
    2/ Sort both collections
    3/ check both collections are the same, index value by index value.


    Any opinions? Any suggestions?

    Cheers,
    Jimmy

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Is there a quick way to compare collections in vba?

    Not sure how you're going to do that, because there's no way to get the keys from a collection. You would need to put the keys in a separate array.

    And if the items were anything other than simple scalar variables, it would be very difficult.

    A dictionary would resolve the first problem, but not the second.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Is there a quick way to compare collections in vba?

    Maybe you could stack them and remove duplicates
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Is there a quick way to compare collections in vba?

    Quote Originally Posted by xladept View Post
    Maybe you could stack them and remove duplicates
    stack them?


    and yeah, my idea is to also remove the duplicates too.

    So is there a quick method for this, or am i going to have to make one?
    Last edited by JimmyWilliams; 09-04-2017 at 02:49 PM. Reason: added more to the thread.

  5. #5
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Is there a quick way to compare collections in vba?

    Quote Originally Posted by shg View Post
    Not sure how you're going to do that, because there's no way to get the keys from a collection.
    My understanding is you need to generate keys while creating the collection, or when adding to a collection. I could do that, but i don't see the point in doing that if for this case.

    Quote Originally Posted by shg View Post
    And if the items were anything other than simple scalar variables, it would be very difficult.
    Nope, easy:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I only have one problem...
    What are you on about?


    I guess that means there is no quick method for checking collections?



    And if the items were anything other than simple scalar variables, it would be very difficult.

    A dictionary would resolve the first problem, but not the second.[/QUOTE]

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Is there a quick way to compare collections in vba?

    Just copy one under the other, with or without the headers

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Is there a quick way to compare collections in vba?

    You are sorting them by item, and your items are strings, which are simple scalar variables. But a collection can contain almost anything as items -- arrays, ranges, other objects, anything except a user-defined type. You would be hard-pressed to sort by that, and it would be harder yet to compare them.

    I assumed you wanted to sort by key (which is always a string for a collection if there is a key), which there is no way to do.

    For your simple case, after sorting, verify that the counts are the same and then compare item by item.
    Last edited by shg; 09-04-2017 at 03:53 PM.

  8. #8
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Is there a quick way to compare collections in vba?

    Quote Originally Posted by shg View Post
    You are sorting them by item, and your items are strings, which are simple scalar variables.
    Ohhhh, yep. I see.

    Quote Originally Posted by shg View Post
    But a collection can contain almost anything as items -- arrays, ranges, other objects, anything except a user-defined type. You would be hard-pressed to sort by that, and it would be harder yet to compare them.
    Yep, yep. makes sense.


    Quote Originally Posted by shg View Post
    I assumed you wanted to sort by key (which is always a string for a collection if there is a key), which there is no way to do.
    Yep, that sounds familiar/makes sense to me.

    ep, yep. makes sense.

    Quote Originally Posted by shg View Post
    For your simple case, after sorting, verify that the counts are the same and then compare item by item.
    That's what I am doing now. However let me ask you another question:
    I remember doing an experiment with collections and their indexes about 6 months ago (cant find the code); I remember concluding that "index numbers aren't fixed and may switch haphazardly in any order".
    Is there a way to know when collection would "randomize"? I have a feeling it might be whenever a new item is added to a collection, or when one is removed from a collection? But I could be wrong.

    My big worry about that is, if I sort them, then the index numbers are all nice and neat.. How long (or until when) can I rely on the collect staying sorted before the index number's become "randomize"?

    I hope that makes sense, if not I can try explaining it again.

    Cheers,
    Jimmy

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Is there a quick way to compare collections in vba?

    "index numbers aren't fixed and may switch haphazardly in any order". Is there a way to know when collection would "randomize"?
    Never. That statement applies to dictionaries, not collections.

+ 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. [Need help quick] How to compare Colours
    By Cessh in forum Excel General
    Replies: 1
    Last Post: 09-22-2015, 05:53 PM
  2. potential collections help
    By Apollo4life in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2014, 08:50 AM
  3. Collections within class module collections
    By AndyLitch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2013, 06:00 AM
  4. [SOLVED] Quick question about automatic row deletion (should be very quick and easy!!)
    By poptart141 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-09-2013, 12:56 PM
  5. [SOLVED] Collections and controls
    By montanaheather in forum Word Formatting & General
    Replies: 13
    Last Post: 12-12-2012, 09:20 AM
  6. Collections problem
    By John in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-21-2006, 01:25 PM
  7. Quick question - quick answer about assigning shortcut keys
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2005, 06:05 AM

Tags for this Thread

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