Dictionary Object: Add range OR array as an item to a corresponding key?

sccrsurfer

Board Regular
Joined
Feb 22, 2011
Messages
61
Hi Guys, can I add a range or an array as an item to a corresponding key? Lets say I have a spreadsheet where Column A has values that populate my dictionary Keys and columns B:D contain the item. So if I used the "Add" method,it would look like this: D.Add Key:=Value Item:Values in the row where the key appears and columns B:D. The goal would be to fill a dictionary with keys in column A, loop through the unique keys, and on another worksheet, dump the items associated w/ each key. Is this possible?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi

No problem in adding an array as a value.

For ex., run:

Code:
Sub test()
Dim dic As Object

Set dic = CreateObject("Scripting.Dictionary")
dic.Add "AAA", Array("Hi", "there", "!")

MsgBox Join(dic("AAA"))
End Sub
 
Upvote 0
Thanks pgc01! For some reason when I hit {Enter} it won't start a new line so my posts will be a bit sloppy. These arrays will be dynamic arrays. So I'm also trying to figure out the best way to fill the array. Should I just do something like D.Add "AAA", Range("B" & row & ":D" & row)?
 
Upvote 0
Hi

You mean adding the values in the range?
Don't forget that if you read the values of a range you get a 2D array.

Code:
Sub test2()
Dim dic As Object
Dim lRow As Long
Dim v As Variant

lRow = 3
Range("B3") = "Hi"
Range("C3") = "there"
Range("D3") = "!"

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "AAA", Range("B" & lRow & ":D" & lRow).Value

v = dic("AAA")

MsgBox v(1, 1) & " " & v(1, 2) & " " & v(1, 3)

End Sub
 
Upvote 0
P. S.

Since in this case your range has only 1 row you could also convert the array from a 2D to a 1D array before you add it to the dictionary.
 
Upvote 0

Forum statistics

Threads
1,224,929
Messages
6,181,812
Members
453,067
Latest member
mdiz777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top