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:
Set dic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(data, 1)
If dic.Exists(data(i, 1)) Then
Q = dic.Item(data(i, 1))
Q = Array(Q, data(i, 2))
dic.Item(data(i, 1)) = Q
Else: dic.Add data(i, 1), data(i, 2)
End If
Next
Bizarrely when I do this and only concatenate the items into a string it works fine- like this:
Set dic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(data, 1)
If dic.Exists(data(i, 1)) Then
Q = dic.Item(data(i, 1))
Q = Q & ", " & data(i, 2)
dic.Item(data(i, 1)) = Q
Else: dic.Add data(i, 1), data(i, 2)
End If
Next
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
Bookmarks