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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,315
Office Version
365
Platform
Windows
Hi
Try
Code:
d.item[COLOR=#ff0000]s[/COLOR](1)
HTH
 

HerrSober

New Member
Joined
Aug 30, 2013
Messages
40
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
 

HerrSober

New Member
Joined
Aug 30, 2013
Messages
40
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:

solotrek

New Member
Joined
Mar 15, 2019
Messages
1
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.
 

Forum statistics

Threads
1,082,258
Messages
5,364,090
Members
400,779
Latest member
lumers

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top