+ Reply to Thread
Results 1 to 18 of 18

Pass dictionary keys into stack object vba

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

    Pass dictionary keys into stack object vba

    Hi Guys,

    I have a code:

    Please Login or Register  to view this content.
    Here i have stack object and dictionary object.

    I am passing whole dictionary into first stack item like here:
    stack item.png

    And now i would like to retrieve second dictionary key from first item in stack so in this case it will be item 1(1) - "ok2".

    How can i do it ?

    I tried to implement methods like

    "Debug.Print CStr(.Pop)(1)(1) but there is an error.

    Please help,
    Jacek Antek

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

    Re: Pass dictionary keys into stack object vba

    No you aren't, that's not what your code does, you're passing an array into the first item of the stack - an array of the keys of the dictionary.

    I can't create that object on my machine, but here's what you want using a collection:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 10-06-2017 at 08:59 AM.

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pass dictionary keys into stack object vba

    You also can't use Pop inside your loop since it will remove the item from the stack. You could use Peek instead
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

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

    Re: Pass dictionary keys into stack object vba

    Hi Kyle,

    thank you for your help and support,

    aa you are right, here i am passing dictionary:

    stack item.png

    the purpose of using stack object is to get the list of values from dictionary in descending order.

    Can I ask you what error do you have ? It is strange...
    Here is more about stack object in VBA:

    http://analystcave.com/excel-vba-dic...ta-structures/

    Best wishes,
    Jacek

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

    Re: Pass dictionary keys into stack object vba

    I understand what a stack is. My Excel hard crashes, I suspect it maybe down to me using 64-bit Excel.

    I fear you may have misunderstood the point of a stack, or indeed what it's for. It will return the order of which things are added to it (in reverse) - it's not for ascending or descending anything (sorting).

    To get values from the dictionary in descending order, you will need to sort the keys. You could do this with a sort mechanism, or you could use something like an arraylist that has a sort built in.

    Additionally, I don't understand the relevance of your screenshot - the stack is empty.

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

    Re: Pass dictionary keys into stack object vba

    Try
    Please Login or Register  to view this content.

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

    Re: Pass dictionary keys into stack object vba

    Thank you Kyle,

    ok it is working now:

    Please Login or Register  to view this content.
    Dic is dictionary here.

    so I am looping throuh dictionary keys and push items to stack from dic.
    And next I am using Array to get reverse order of dictionary items.

    Dictionary is created like that:

    8, 3, 2, 1 and has to be reversed for 1, 2, 3, 8.

    So this function return array with reversed order.

    I was just curious it is possible to (based on xlnitwit code) add for each new stack's item one key from dictionary in second way.

    Thank you Guys,
    Jacek
    Last edited by jaryszek; 10-06-2017 at 09:33 AM.

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

    Re: Pass dictionary keys into stack object vba

    You realise that dictionaries don't guarantee order right? That's why you can't loop through them - that's why you can only access by key.

    If you need to guarantee order, you need to use another data structure, a collection or array for example.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pass dictionary keys into stack object vba

    Quote Originally Posted by Kyle123 View Post
    You realise that dictionaries don't guarantee order right?
    I know that is the official position but can I ask whether you have ever experienced the order actually changing? I have to say that I have not.

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

    Re: Pass dictionary keys into stack object vba

    No, but I wouldn't rely on it

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pass dictionary keys into stack object vba

    I have never needed to, I was merely curious as to your experience. Thank you.

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

    Re: Pass dictionary keys into stack object vba

    TBH I'm not convinced that it ever will, I've certainly never encountered it. The issue is that you can overwrite values in a dictionary, you can't with a collection.

    This means that the order of values can change in a dictionary, it can't in a collection unless explicitly inserted/removed - they'll always be in which the order they added.

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

    Re: Pass dictionary keys into stack object vba

    Thank you Kyle123,

    yes maybe you have right but i am using the same thing (dictianary + array bubble sorting) in Excel and never users have had any problems connected with this.
    In dictionary items are added one by one and there is no chance to change the order.

    Thank you,
    Best wishes,
    Jacek

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

    Re: Pass dictionary keys into stack object vba

    Debug.Print CStr(.Peek().keys()(i - 1))
    xlnitwit could you please explain what keys method is doing here?

    It is peek method yes ?

    Sorry i have to understand it.

    Best wishes,
    Jacek

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

    Re: Pass dictionary keys into stack object vba

    Up

    Jacek Antek

  16. #16
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pass dictionary keys into stack object vba

    Peek returns the Dictionary object without removing it from the stack. Keys returns an array of the keys from the Dictionary.

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

    Re: Pass dictionary keys into stack object vba

    thank you xlnitwit !

    Best Wishes and have a nice day,
    Jacek

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

    Re: Pass dictionary keys into stack object vba

    I suspect that something like the following is more efficient:
    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. How to pass dictionary from function into main sub?
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2017, 07:12 AM
  2. [SOLVED] using keys of the dictionary
    By maroon1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2016, 03:04 PM
  3. [SOLVED] How to compare keys in one dictionary to keys in another dictionary
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2015, 02:27 PM
  4. Storing a Dictionary into a Dictionary Object
    By pago_boss in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2015, 03:04 AM
  5. [SOLVED] Filter data using keys in dictionary
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2014, 11:48 PM
  6. [SOLVED] Transposing dictionary keys and items into one column
    By strud in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2013, 09:42 AM
  7. How do the experts pass an error up the call stack?
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2013, 05:23 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