+ Reply to Thread
Results 1 to 12 of 12

how to get the key from a collection

  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 get the key from a collection

    i have the following that prints the item in a collection:

    Please Login or Register  to view this content.
    how can i print the key from the collection. probably simple but i have searched for and not found an answer on the web.

    thoughts?

  2. #2
    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 get the key from a collection

    any help for printing the key from a collection?

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,792

    Re: how to get the key from a collection

    Administrative Note:

    We don't expect you to bump threads more than once in 24 hours - it has been only an hour since you posted. Please remember that those who help here do so voluntarily and of their own goodwill. They live in many different time zones and many will be out at work right now if they are not asleep on the other side of the globe. This is not a paid 'service': members will help when they are ready and able to do so, and not 'on demand'. Please do not try to put pressure of time on anyone here: if you get the help you need today, that's fine, but you really should not count on it. Thanks for your understanding and patience.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  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 get the key from a collection

    sorry Ali, will do in the future.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,792

    Re: how to get the key from a collection

    Thank you.

  6. #6
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: how to get the key from a collection

    Untested
    Please Login or Register  to view this content.

  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: how to get the key from a collection

    No es posible. You can't retrieve the key by item (it could be a one-to-many mapping), and you can't iterate the keys.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    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: how to get the key from a collection

    I should add that when I need to do this, I skip storing a key for the item and store instead a {'key', item} pair:

    Please Login or Register  to view this content.
    That allows you to retrieve the 'key' by index (e.g., col.Items(n)(0)), but you still can't retrieve 'key' by item, because it can still be one to many:

    Please Login or Register  to view this content.
    What's the key associated with Item = 1?
    Last edited by shg; 04-12-2019 at 12:29 PM.

  9. #9
    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 get the key from a collection

    @shg, thanks for that input. acceptable solution. printed out the entire range with the following code:

    Please Login or Register  to view this content.

  10. #10
    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: how to get the key from a collection

    You're welcome.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: how to get the key from a collection

    You could also use a Dictionary instead of course.
    Rory

  12. #12
    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: how to get the key from a collection

    I use both, but collections are great for their ability to be accessed by index -- like when using them as a stack, or for an insertion sort.
    Last edited by shg; 04-12-2019 at 10:21 PM.

+ 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. [SOLVED] When a collection created bring another column value into the collection
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2018, 04:33 AM
  2. [SOLVED] DVD Collection
    By John775 in forum Excel General
    Replies: 3
    Last Post: 10-03-2018, 07:16 AM
  3. [SOLVED] Adding a collection to a collection.
    By Max_Taylor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2018, 12:22 AM
  4. Replies: 4
    Last Post: 10-30-2015, 01:30 PM
  5. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  6. Replies: 2
    Last Post: 12-23-2011, 11:04 AM
  7. Problems storing a Collection within a Collection. Is this possible?
    By Tarball in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2007, 10:47 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