+ Reply to Thread
Results 1 to 19 of 19

If dictionary key exists add new item to existing item array

  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    If dictionary key exists add new item to existing item array

    I am having trouble adding items to a scripting dictionary in the way I want.

    Basically, if the dictionary key exists then I want to add the corresponding item to the items already added for that key - in an array.

    So far I have it working, but it only works up until two items and then just 'writes over'/ the previous:

    Please Login or Register  to view this content.
    Bizarrely when I do this and only concatenate the items into a string it works fine- like this:

    Please Login or Register  to view this content.
    But unfortunately a concatenated string is unsuitable for my needs. Any ideas on how I can add them into the array as I wish?

    Thanks in advance

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: If dictionary key exists add new item to existing item array

    Have you tried retrieving the array for the item from the dictionary, resizing it and then putting it back?
    If posting code please use code tags, see here.

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

    Re: If dictionary key exists add new item to existing item array

    Array doesn't work like that, you'd be better off having a dictionary in each item and pushing into that - so you have a dictionary of dictionaries

  4. #4
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Quote Originally Posted by Kyle123 View Post
    Array doesn't work like that, you'd be better off having a dictionary in each item and pushing into that - so you have a dictionary of dictionaries
    Thanks Kyle I didn't realise I could not do that with arrays- I am learning VBA by doing so...

    Anyway I think I can make it work by using a dictionary in each item, thanks!

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

    Re: If dictionary key exists add new item to existing item array

    Quote Originally Posted by strud View Post
    I clicked solved a bit preemptively on my post about dictionaries of dictionaries.

    I am not quite sure how having the item as a dictionary will help my situation, because I don't know how many items I wanted to add to the array, and I still want the items to be assigned to specific keys.
    Sure, since you don't have an array you don't need to worry about how many items you want to add:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    For those interested, aside from the dictionaries I also manged to get the same effect here- except there is duplication of the keys- but that was suitable for my needs:

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Quote Originally Posted by Kyle123 View Post
    Sure, since you don't have an array you don't need to worry about how many items you want to add:
    Please Login or Register  to view this content.
    Thanks a lot Kyle!

  8. #8
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Quote Originally Posted by Kyle123 View Post
    Sure, since you don't have an array you don't need to worry about how many items you want to add:
    Sorry Kyle I am still confused - how would I now access & transpose the keys and items?

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

    Re: If dictionary key exists add new item to existing item array

    Like you would normally:
    Please Login or Register  to view this content.
    So that will return an array of all the items with a key of the first distinct item

  10. #10
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Perfect, that's got it. I think I was over complicating it in my head. Thanks again!

  11. #11
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: If dictionary key exists add new item to existing item array

    as an option
    Please Login or Register  to view this content.

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

    Re: If dictionary key exists add new item to existing item array

    What if the data contains ~?


    Sent from my iPhone using Tapatalk

  13. #13
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Thanks guys, I am probably being stupid here, but how would I get a list of all keys from the main dictionary with their items transposed in the columns adjacent?

    So it would be all the keys from data(i, 1) and their items from (i, 2)...

    I can't figure it out from what you have given me..

  14. #14
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: If dictionary key exists add new item to existing item array

    Quote Originally Posted by Kyle123 View Post
    What if the data contains ~?
    Very unlikely, but, yes it can happen

  15. #15
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: If dictionary key exists add new item to existing item array

    Hi strud,

    maybe a small sample

  16. #16
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Ok attached- I need to be able to add items in an array I think so I can transpose them in the desired way
    Attached Files Attached Files

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: If dictionary key exists add new item to existing item array

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

  18. #18
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: If dictionary key exists add new item to existing item array

    Perfect Norie thanks dude. I just couldn't work out hot adapt what they had given me.

    Thanks again!

  19. #19
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: If dictionary key exists add new item to existing item array

    or
    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. [SOLVED] accesing class members of an object returned by dictionary item
    By vientito in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2014, 05:08 PM
  2. Remove All Instances Of Key Item Pairs From Dictionary
    By goss in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-31-2013, 12:37 AM
  3. 2 Dimentional Array as Dictionary Item
    By erock24 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-05-2011, 11:13 AM
  4. [SOLVED] Match return #NA ...though item exists!
    By Sige in forum Excel General
    Replies: 2
    Last Post: 01-12-2006, 10:50 AM
  5. Dictionary object: Error assigning user defined data type to item
    By Paul Urbanus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2005, 12:25 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