|
-
May 24th, 2013, 02:27 PM
#1
Thread Starter
PowerPoster
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
-
May 25th, 2013, 04:53 AM
#2
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
-
May 25th, 2013, 05:37 AM
#3
Thread Starter
PowerPoster
Re: FILL dictionary
 Originally Posted by westconn1
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.
-
May 25th, 2013, 06:20 AM
#4
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
-
May 25th, 2013, 07:19 AM
#5
Thread Starter
PowerPoster
Re: FILL dictionary
 Originally Posted by westconn1
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...
-
May 25th, 2013, 06:18 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|