Retrieving items from scripting.dictionary

HerrSober

New Member
Joined
Aug 30, 2013
Messages
40
Sub MyQuestion()

' First populate the scripting.dictionary
Dim d
Set d = CreateObject("Scripting.Dictionary")

d.Add "a", "Athens" d.Add "b", "Belgrade"
d.Add "c", "Cairo"


Debug.Print d.Item(1) ' Why does this return blank?

' How can I retreive items from this dictionary?
' I am very grateful for any tips and hints! :)

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi
Try
Code:
d.item[COLOR=#ff0000]s[/COLOR](1)

HTH
 
Upvote 0
Hi
Try
Code:
d.item[COLOR=#ff0000]s[/COLOR](1)

HTH

Thanks for your attempt.

I still didn't get any result with "d.items(1)" with the code I posted. When I tried with the collections, I got it working (see code under. I edited the variables on the code under to make it easier to read.)


Code:
Sub With_Collection()
   
   Dim d As Collection
    Set d = New Collection
    d.Add "Athens", "1"
    d.Add "Belgrade", "2"
    d.Add "Cairo", "3"

    Debug.Print d.Item(2)   'This code returns "Belgrade"


End Sub

But the same code with scripting.dictionary didn't work.

The reason I want to get scripting.dictionary to work is that I am working on an old code where the previous programmer used this object. I have otherwise understood that it runs faster than normal arrays.

Code:
Sub With_ScriptingDictionary()

Dim d
Set d = CreateObject("Scripting.Dictionary")

d.Add "Athens", "1"
d.Add "Belgrade", "2"
d.Add "Cairo", "3"

Debug.Print d.Items(1)  ' This code returns this errormessage:

'Run-time error '451'
'"Property let procedure not defined and property get procedure did not return an object"

End Sub
 
Upvote 0
Looks like I finally got it working now! :biggrin:

Code:
Sub With_ScriptingDictionary_Working()

Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
Dim i As Integer

d.Add 1, "Athens"
d.Add 2, "Belgrade"
d.Add 3, "Cairo"

Debug.Print d.Item(2)  ' Returns "Belgrade"

End Sub

'And then it works for the type of loop I wanted to use it for: :)

Code:
For i = 1 To d.Count
Debug.Print d.Item(i)
Next i
' Returns "Athens", "Belgrade" and "Cairo"
 
Last edited:
Upvote 0
Sub MyQuestion()

' First populate the scripting.dictionary
Dim d
Set d = CreateObject("Scripting.Dictionary")

d.Add "a", "Athens" d.Add "b", "Belgrade"
d.Add "c", "Cairo"


Debug.Print d.Items()(1)

' How can I retreive items from this dictionary?
' I am very grateful for any tips and hints! :)

End Sub

Please see updated line above. Worked for me. Items needs() then index number.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

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