Results 1 to 6 of 6

Thread: FILL dictionary

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,932

    FILL dictionary

    I have thi sheet and 2 column

    AAAAA 123456
    AAAAA 128456
    AAAAA 123457
    BBBBB 123456
    BBBBB 128863
    BBBBB 129226
    CCCCC 234556

    i need to loop all cells into sheet and fill a dictionary in this mode:

    AAAAA not exists add, AAAAA and related value 123456

    AAAAA just exists, add 128456 to the already exists combination

    ecc


    to the and of cell loop the dictionary filled

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FILL dictionary

    AAAAA just exists, add 128456 to the already exists combination
    as a dictionary key can only have one item, the example uses a tab delimited string
    Code:
    For Each c In Range("a:a")
        If IsEmpty(c) Then Exit For  ' stop on empty cell
        If Not d.Exists(Trim(c.Value)) Then d(Trim(c.Value)) = c.Offset(, 1).Text Else d(Trim(c.Value)) = d(Trim(c.Value)) & vbTab & c.Offset(, 1).Value
    Next
    where d is a scripting dictionary object
    this is tested using your sample data, trim may not be required, but solved some issue i was having
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,932

    Re: FILL dictionary

    Quote Originally Posted by westconn1 View Post
    as a dictionary key can only have one item, the example uses a tab delimited string
    Code:
    For Each c In Range("a:a")
        If IsEmpty(c) Then Exit For  ' stop on empty cell
        If Not d.Exists(Trim(c.Value)) Then d(Trim(c.Value)) = c.Offset(, 1).Text Else d(Trim(c.Value)) = d(Trim(c.Value)) & vbTab & c.Offset(, 1).Value
    Next
    where d is a scripting dictionary object
    this is tested using your sample data, trim may not be required, but solved some issue i was having
    NICE!

    But how to looping and retrive data from the dictionary collection,

    in effect i need to loop for all item the related key...


    i think with a 2 for two for next cicles...but how to?
    Last edited by luca90; May 25th, 2013 at 06:06 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FILL dictionary

    there is an example from microsoft here http://support.microsoft.com/kb/187234

    in effect i need to loop for all item the related key...
    as i specified above each key can only have a single item, so using the previous example, try like
    Code:
    items = split(d("AAAAA"), vbtab)
    for each i in items
      debug.print i
    next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,932

    Re: FILL dictionary

    Quote Originally Posted by westconn1 View Post
    there is an example from microsoft here http://support.microsoft.com/kb/187234

    as i specified above each key can only have a single item, so using the previous example, try like
    Code:
    items = split(d("AAAAA"), vbtab)
    for each i in items
      debug.print i
    next
    Have you tested ....?

    many error in definition of variable...

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: FILL dictionary

    many error in definition of variable
    you can fix, i also created a dictionary object to work with
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width