+ Reply to Thread
Results 1 to 15 of 15

Dictionary find index of current key

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Dictionary find index of current key

    Hi,

    i am looping through dictionary like here:

    Please Login or Register  to view this content.
    It is possible to get index of current key?

    The idea is to get item not from current dicKey key but from the next key wthihin provided loop.

    Example:

    dic.add 1, Paul
    dic.add 2, Luke
    dic.add 3, Jacek
    dic.add 4, Piter

    So if dicKey = 3 and corresponding item = Jacek, i want to get here Piter (3 key + 1 = 4th key and correspoding item).

    Please help,
    Best,
    Jacek

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Dictionary find index of current key

    dic.items()(3)

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    ok thank you.

    so simple i can add counter and know which current key i am looping.

    Best,
    Jacek

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,362

    Re: Dictionary find index of current key

    Or like this, where i is the current key.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Dictionary find index of current key

    Be aware that since dictionaries can be updated their order cannot be guaranteed. If you need this, you'd be better off using:
    Please Login or Register  to view this content.
    Then referring only to tmp (or a collection)

    Also, calling items/keys on a dictionary is slow, so you're better off not doing it in a loop if you have a lot of entries.

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    thank you Guys,

    Kyle123,

    what do you mean by:
    tmp = dic.Keys()?

    you want to create array from dictionary?

    their order cannot be guaranteed
    ok this is interesting. If i will change dic.items()(3) = "NewName" i can change the order?

    Jacek

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Dictionary find index of current key

    you want to create array from dictionary?
    What do you think that:
    Please Login or Register  to view this content.
    is doing?

    It's the same as:
    Please Login or Register  to view this content.
    You're creating an array and referencing it. It's better to create the array once than in the loop

  8. #8
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    Thank you.

    i just wonder how can i change order in dictionary after updating?
    I have never done this by accident.

    I would like to be prepared for this issue in the future but i have to fully understand this.

    dic.items()(3)
    this is showing correspoding item to key number = 3

    Best,
    Jacek

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Dictionary find index of current key

    Quote Originally Posted by jaryszek View Post
    ok this is interesting. If i will change dic.items()(3) = "NewName" i can change the order?
    No, if you use the overwrite method of adding to a dictionary, you update the value at that key, for a fuller description, see:
    https://stackoverflow.com/questions/...of-item-insert

    It's one of the differences between collections and dictionaries

  10. #10
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    thank you Kyle123,

    interesting link and well explained how overwrite method is working.

    Be aware that since dictionaries can be updated their order cannot be guaranteed
    sorry but still i do not understand this. So how can i change the order? it is not possible?

    Jacek

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,231

    Re: Dictionary find index of current key

    Is this solved or not???
    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. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  12. #12
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    I changed the topic to not solved.

    Or maybe Ali do you want me to create new topic?

    Best,
    Jacek

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Dictionary find index of current key

    It depends what you mean by change the order, if you mean values, then you can change the order like this
    Please Login or Register  to view this content.
    The key remains in place, but the value changes.

    It depends on your usecase and it may make no difference to you, the link explains it better than I can, but something to be aware of. It may be easier to think of dictionaries as what they actually are a collection of key and value pairs, that just happen when iterating over them to output in the same order as the input. It's more correct to say that keys are always ordered whereas values can be updated so may not be what you expect them to be

    You cannot do this with simple types in collections so collections can be considered more ordered. It's why you can insert at positions in collections and not in dictionaries.
    Last edited by Kyle123; 04-25-2019 at 05:34 AM.

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,231

    Re: Dictionary find index of current key

    Quote Originally Posted by jaryszek View Post
    Or maybe Ali do you want me to create new topic?
    Absolutely not - it is NOT a new topic!

    Thanks for removing the SOLVED tag - you will not be ignored now.

  15. #15
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Dictionary find index of current key

    Thank you Kyle123,

    now i understand. Great example and explanation,

    thank you and have a nice day!

    Jacek

+ 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. Replies: 1
    Last Post: 01-15-2019, 06:17 PM
  2. Why dictionary very slow ,is dictionary have maksimum data
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-30-2018, 03:57 PM
  3. [SOLVED] Input number from dictionary to Application.index
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2018, 02:03 AM
  4. [SOLVED] Dictionary or Scripting.Dictionary. Binding Referencing Dim-ing. Sub routines and Function
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-12-2016, 08:28 AM
  5. Dictionary - Using a dictionary of dictionaries to hold individual orders
    By wazimu13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2015, 08:32 PM
  6. [SOLVED] Tweak Macro to find Matches instead of unique use dictionary
    By capson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2014, 03:06 PM
  7. Dictionary-like Excel spreadsheet (SMALL(IF(Index...,ROW()))
    By sampahmel in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-14-2014, 10:38 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