Hi,
Forgive me if this is a simple question. I am a novice and have been battling with a dictionary item retrieval issue for quite a while, spent days searching the internet for an answer and have given up in favour of posting my own question. Any help will be very much appreciated.
My issue is I cannot retrieve the data indexed by a particular key. Example code:
Dim Dict01 As New Dictionary
Sub Macro1()
'
' Macro1 Macro
'
'
Dim Count As Integer
Dim CurCell As Range
Dim Keys As Variant
Dim Iterate As Variant
Dim Test As Variant
Set Dict01 = New Dictionary
'
' Row 1 is where the dictionary keys live.
' Row 2 and onward is where the data referenced by the keys is located.
'
Range("A1").Select
Set CurCell = Range(Selection, Selection.End(xlToRight))
CurCell.Select
'
' Creates dictionary keys
'
For Each Iterate In CurCell
Dict01(Iterate) = ""
Next
Keys = Dict01.Keys
Count = 0
'
' Selects data for loading into dictionary
'
Range("A2").Select
Set CurCell = Range(Selection, Selection.End(xlToRight))
CurCell.Select
For Each Iterate In CurCell
Dict01(Keys(Count)) = Iterate
Count = Count + 1
Next
Test = Keys(0)
MsgBox (Dict01.Item(Keys(0))) ' Works! returns data identified but 1st key
MsgBox Test 'Proves Test contains correct key
MsgBox (Dict01.Item(Test)) ' Does not work! displays empty content
End Sub
I do not understand why the last Msgbox statement returns 'nothing' rather than the data held by Keys(0).
For completeness, the data in the sheet would look like this:
A B C
1 Ford Audi BMW
2 Ka A5 X6
I expect the 3 Msgbox's to return
Ka - Output as expected
Ford - Output as expected
Ka - No output.
Many thanks in advance.
Forgive me if this is a simple question. I am a novice and have been battling with a dictionary item retrieval issue for quite a while, spent days searching the internet for an answer and have given up in favour of posting my own question. Any help will be very much appreciated.
My issue is I cannot retrieve the data indexed by a particular key. Example code:
Dim Dict01 As New Dictionary
Sub Macro1()
'
' Macro1 Macro
'
'
Dim Count As Integer
Dim CurCell As Range
Dim Keys As Variant
Dim Iterate As Variant
Dim Test As Variant
Set Dict01 = New Dictionary
'
' Row 1 is where the dictionary keys live.
' Row 2 and onward is where the data referenced by the keys is located.
'
Range("A1").Select
Set CurCell = Range(Selection, Selection.End(xlToRight))
CurCell.Select
'
' Creates dictionary keys
'
For Each Iterate In CurCell
Dict01(Iterate) = ""
Next
Keys = Dict01.Keys
Count = 0
'
' Selects data for loading into dictionary
'
Range("A2").Select
Set CurCell = Range(Selection, Selection.End(xlToRight))
CurCell.Select
For Each Iterate In CurCell
Dict01(Keys(Count)) = Iterate
Count = Count + 1
Next
Test = Keys(0)
MsgBox (Dict01.Item(Keys(0))) ' Works! returns data identified but 1st key
MsgBox Test 'Proves Test contains correct key
MsgBox (Dict01.Item(Test)) ' Does not work! displays empty content
End Sub
I do not understand why the last Msgbox statement returns 'nothing' rather than the data held by Keys(0).
For completeness, the data in the sheet would look like this:
A B C
1 Ford Audi BMW
2 Ka A5 X6
I expect the 3 Msgbox's to return
Ka - Output as expected
Ford - Output as expected
Ka - No output.
Many thanks in advance.
Last edited: