+ Reply to Thread
Results 1 to 35 of 35

Retrive multiple items from dictionary

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

    Retrive multiple items from dictionary

    Hi Guys,

    This is similar post to this one:

    https://www.mrexcel.com/forum/excel-...ml#post4754332

    but this is not cross posting.

    my code is:

    Please Login or Register  to view this content.
    So as you can see we have one key: "Key1" and a few items attached to it (1,2,3,4).

    How to loop and retrive all items from all keys within dictionary?

    Best wishes,
    Jacek Antek

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Retrive multiple items from dictionary

    Hi,
    Try this way:
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    Thnank you kasan!

    This is working.

    What if you have:

    Please Login or Register  to view this content.
    2 keys with different collections?


    Something like this?:

    Please Login or Register  to view this content.
    Best wishes,
    Jacek Antek

  4. #4
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Retrive multiple items from dictionary

    How about:
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    Wow excellent! You are brilliant man!

    One more question:

    It is possible to add value to specific item attacked to key?:

    Please Login or Register  to view this content.
    So for example i would like to change value "3" in collection coll for value "2".

    Or only one possible way is to remove items form key and add once again new collection into dictionary ?

    Best wishes ,
    Jacek Antek

  6. #6
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Retrive multiple items from dictionary

    I think it's not possible to do it like you wrote.
    If it would be arrays (not collections) then I would do something like this (not tested):
    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,940

    Re: Retrive multiple items from dictionary

    Unfortunately this is not working with reference to coll ;/

    Please Login or Register  to view this content.
    I have checked if this will be working with arrays and yes.
    If you have array in dictionary this code will work.

    Jacek Antek

  8. #8
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Retrive multiple items from dictionary

    May be some other experts/gurus can give you solution for collections. Interesting for me too.

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

    Re: Retrive multiple items from dictionary

    I was thinking about doing the class with array inside and reference to it.

    But i can not do it unfortunately...


    Jacek

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

    Re: Retrive multiple items from dictionary

    Try change to
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    Thank you jindon but this code is already working good:

    Please Login or Register  to view this content.
    Now we are thinking how to pass collection to array from dictionary.

    Jacek

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

    Re: Retrive multiple items from dictionary

    In that case I would not use Collection.
    see
    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Retrive multiple items from dictionary

    Probably you will need to loop through collection and add each element to array.
    Using function this may looks like:

    Please Login or Register  to view this content.
    Function:
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    Thank you Guys.

    Maybe this is the solution for this.

    jidon you are very close:

    Please Login or Register  to view this content.
    What if we have 2 keys here and I would like to refer only for one of them ?

    Jacek Antek

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

    Re: Retrive multiple items from dictionary

    You can change them to
    Please Login or Register  to view this content.
    And you can convert all the items in an array ONLY IF all the array is exactly the same size.
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    Ok I have found this !

    The key code here is:

    Please Login or Register  to view this content.
    Thanks to it you can assign items to an array, add values within array and return it into dictionary !!!

    Thank you veru much
    Jacek

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

    Re: Retrive multiple items from dictionary

    See my last post that you just missed.

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

    Re: Retrive multiple items from dictionary

    Wow this is very advance solution. Thank you !

    So classes are not neccessary here?

    It is a pity because i would like to learn it from you ;-)


    Best Wishes
    Jacek Antek

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

    Re: Retrive multiple items from dictionary

    I use Array, Dictionary, ArrayList, SortedList in my code, so if you search my posts, you will find many examples.

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

    Re: Retrive multiple items from dictionary

    Hi Guys,

    it's me again.

    This is not working good unfortunately.

    My Code is :

    Please Login or Register  to view this content.
    In attachment you can see my problem:

    Bez*tytu?u.jpg

    So in this case
    Please Login or Register  to view this content.
    varArray will be connected with item 1 from collection, and i want to get item 2.

    So i would like to get the index of key from dictionary to know which items which specific keys are assigned to dimension in array.
    So i want to take only the specific dimension of array (it can be 1, 2 ,3 ....n depending on key).

    It is possible ?

    Example:
    I have 5 keys of dictionary and giving:
    varArray = Application.Index(Dict_People.items, 0, 0)

    I will get the first items from the first key. I want to get items from the key which I am actually processing (not the first one, it can be 2,3 or n key).

    Please help,
    Best wishes
    Jacek Antek
    Last edited by jaryszek; 02-14-2017 at 04:00 AM.

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

    Re: Retrive multiple items from dictionary

    Hello,

    I solved the problem,

    I simply added one dictionary and after operations with array - I am clearing it.

    my Code:

    Please Login or Register  to view this content.
    I think that is not the best solution for this,
    maybe you Gurus have any other ideas ?

    Best Wishes,
    Jacek Antek

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

    Re: Retrive multiple items from dictionary

    Anyone ?

    Jacek

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

    Re: Retrive multiple items from dictionary

    Quote Originally Posted by jindon View Post
    You can change them to
    Please Login or Register  to view this content.
    And you can convert all the items in an array ONLY IF all the array is exactly the same size.
    Please Login or Register  to view this content.

    jindon, please help.

    I am using your code:

    Please Login or Register  to view this content.
    Problem is when I am assigning array into dictionary : I am loosing type Variant/Object/Dictionary !! It is only Variant/Variant and I even do not have binarycode like here:

    binarymode.jpg

    What can i do in order to assign array to dictionary item like dictiorary object, not variant?

    And at all method to retrive item :

    Please Login or Register  to view this content.
    is not working because keys are not found!

    Pleeease help,
    Best Wishes,
    Jacek Antek
    Attached Images Attached Images
    Last edited by jaryszek; 02-22-2017 at 09:46 AM.

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

    Re: Retrive multiple items from dictionary

    If you upload a small sample workbook that clearly shows the logic about before/after, I can comment.

    I can not help reading the code that is not working...

  25. #25
    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: Retrive multiple items from dictionary

    Quote Originally Posted by jaryszek View Post
    Problem is when I am assigning array into dictionary : I am loosing type Variant/Object/Dictionary !! It is only Variant/Variant
    Hi,

    Your varArray is a Variant containing a Variant array, and that is what you assign to the dictionary item, therefore that is what you see as its Type. The Item is not a Dictionary or Object.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

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

    Re: Retrive multiple items from dictionary

    Hi Guys,

    sorry for late reply.

    In attachment there is my macro (PIT-liczenie v3).

    Steps which should be run:

    1. Run module Main in PIT-liczenie v3.
    2. Indicate two files - "2016-08-03" and "2016-12-02":
    - macro will be go through all sheets in each workbook and search for number in column G,
    - if number is find is adding it to dictionary with sum,
    - if key exists we are get from dictionary array, makes changes (sums adding) and assign this array to dictionary,
    - first workbook is going fine,
    - problem occurs where we will go to second workbook - there is a problem with searching for key in dictionary.
    As I wrote before - code
    Please Login or Register  to view this content.
    is assigning array to dictionary and later key within dictionary is not searched.
    But why only within second workbook?

    I have noticed that when I am not making changes with array and I am not passing it to dictionary, Type for specific item will remain "Variant/Object/Dictionary" and thanks to it key.exists method is working.
    Maybe there is another way to assign array into dictionary as Type "Variant/Object/Dictionary"? Why if we have array items in dictionary with Type "Variant" method key.exists doesnt work?

    Please help me Guys,
    Best Wishes,
    Jacek Antek
    Attached Files Attached Files

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

    Re: Retrive multiple items from dictionary

    Is this what you are trying?
    Please Login or Register  to view this content.

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

    Re: Retrive multiple items from dictionary

    jindon,

    Thank You very much !!!

    I thought that i can VBA code but Looking at yours - I think that I can't this.

    Yes, it is exactly what i Meant to do.

    I have couple of questions about it:

    1.
    Please Login or Register  to view this content.
    What this code do? It is creating for key (a(i, 3)) the dictionary yes?

    2. This method:
    Please Login or Register  to view this content.
    What we are doing here?
    I think that for this dictionary(dic) within dictionary(a(i, 3)) we are assigning item txt and adding to it sum?

    3. For what is this "n"?
    Please Login or Register  to view this content.
    4. what means ": n = 0" in
    Please Login or Register  to view this content.
    Ufff your code is master code!

    Thank you in advance for help,
    Jacek Antek

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

    Re: Retrive multiple items from dictionary

    Wow and this one is also very confusing for me:

    Bez*tytu?u.jpg

    What does it means?

    Jacek

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

    Re: Retrive multiple items from dictionary

    Wow and this one is also very confusing for me:

    "GalembaJolanta"

    This strange sign which means that there is more then one item...

    What does it means?

    Jacek

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

    Re: Retrive multiple items from dictionary

    1
    Please Login or Register  to view this content.
    As you see dic is the main dictionary to store unique item from col.G.
    Once it finds unique item, set "child" dictionary for its item. dic key:= col.G, item:= createobject("scripting.dictionary")
    2)
    Please Login or Register  to view this content.
    txt= a(i,1) & chr(2) & a(i,2)
    Why chr(2)?, make it safer when concatenate 2 strings with the character that we merely use.
    e.g.
    If string1 = "A" and string2 = "BC", it will also equivalent to string1="AB" and string2 = "C", both "ABC"...
    Now, dic(a(i,3)) = dictionary only for a(i,3), so when it finds txt, it adds a(i,6) to its item.
    dic(a(i,3)).Add txt, a(i,6) for the first time and dic(a(i,3))(txt) + 1(i,6) to add to its item.

    3)
    Please Login or Register  to view this content.
    This is to count max number of keys(txt) for overall child dictionary to determine the enough size of 1st dimension of output array.

    4) n=0 to initialize row counter for output array...

    Further question?

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

    Re: Retrive multiple items from dictionary

    Thank you jindon once more time for your help and support !

    Now i am understanding all !!!!

    And learnt a lot of thing about dictionaries and not only about them...

    Thank you !

    Best Regards,
    Jacek Antek

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

    Re: Retrive multiple items from dictionary

    Very nice and thanks for the rep.

    Glad if it helps to widen your knowledge about coding.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

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

    Re: Retrive multiple items from dictionary

    Ok i will do that.

    One more thing,

    a = ws.Range("e2:j154").Value is changing a variable from long to array.
    It is not better to dim a as variant at the beginning?

    Why? It is standard? Maybe is the fastest solution?

    Jacek

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

    Re: Retrive multiple items from dictionary

    Of course.
    Please Login or Register  to view this content.
    Any variable without its data type is Variant type.

+ 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. Add items to dictionary - VBA Help Needed
    By mitko007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2015, 07:26 AM
  2. [SOLVED] Looping through dictionary items where items are an array
    By strud in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2014, 06:56 AM
  3. [SOLVED] Referencing multiple dic.items in dictionary to clean up my code
    By strud in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-15-2013, 07:58 AM
  4. [SOLVED] Add multiple items to scripting dictionary from one key and keep structure
    By strud in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-07-2013, 09:44 AM
  5. [SOLVED] Adding items to Dictionary object
    By Eduard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-03-2013, 04:39 AM
  6. Nested dictionary items, how do you access them?
    By treyr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2010, 12:23 PM
  7. looping through items in a dictionary
    By Abe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-08-2006, 05:45 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