Dictionary: Item not being returned

pvr928

Well-known Member
Joined
Oct 21, 2002
Messages
790
Hi

I've created a dictionary, and then converted that dictionary into an array.

I am trying to retrieve the Item for the dictionary using a known Key.

I have used the code:

Code:
avKeys = dict.Keys

where avKeys is a Variant and dict is a Dictionary.

I am attempting to use a string (sSearchTerm) to return the Item, and:

avKeys(1) = aSearchTerm (say "1456.txt" (avKeys(1)) = "1456.txt" (aSearchTerm))

However,

Code:
Debug.Print dict(avKeys(1)) = 2

but

Code:
Debug.Print dict(aSearchTerm) = [Empty]

What am I missing if avKeys(1) = aSearchTerm (I've even tested it via the following:

Code:
   If avKeys(1) = aSearchTerm Then
    
        MsgBox "True"
        
   Else
    
        MsgBox "False"
        
    End If

Any help greatly appreciated.

Cheers

pvr928
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try Stepping through the code below, based on data shown, to see results:-
[TABLE="width: 68"]
<colgroup><col width="27" style="width: 20pt; mso-width-source: userset; mso-width-alt: 967;"> <col width="64" style="width: 48pt;"> <tbody>[TR]
[TD="class: xl63, width: 27, bgcolor: #DAEEF3"] [/TD]
[TD="class: xl63, width: 64, bgcolor: #DAEEF3"]A[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: #DAEEF3, align: right"]1[/TD]
[TD="class: xl64, bgcolor: #DCE6F1"]Data1[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: #DAEEF3, align: right"]2[/TD]
[TD="class: xl65, bgcolor: white"]Data2[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: #DAEEF3, align: right"]3[/TD]
[TD="class: xl64, bgcolor: #DCE6F1"]Data3[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: #DAEEF3, align: right"]4[/TD]
[TD="class: xl65, bgcolor: white"]Data4[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: #DAEEF3, align: right"]5[/TD]
[TD="class: xl64, bgcolor: #DCE6F1"]Data5[/TD]
[/TR]
</tbody>[/TABLE]

Code:
[COLOR="Navy"]Sub[/COLOR] MG28Jan52
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Dict [COLOR="Navy"]As[/COLOR] Object, Avkeys [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Dict = CreateObject("scripting.dictionary")
Dict.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    '[COLOR="Green"][B]Dn.address added as dict.item[/B][/COLOR]
    Dict(Dn.Value) = Dn.Address
[COLOR="Navy"]Next[/COLOR]
'[COLOR="Green"][B]One dimension Array[/B][/COLOR]
Avkeys = Dict.Keys
MsgBox Avkeys(3)
MsgBox Dict(Avkeys(3))
MsgBox Dict("Data4")
'[COLOR="Green"][B]Two dimension array[/B][/COLOR]
Ray = Application.Transpose(Array(Dict.Keys, Dict.items))
MsgBox Ray(2, 1)
MsgBox Ray(2, 2)
MsgBox Dict(Ray(2, 1))
MsgBox Dict("Data2")
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi MickG

Thank you so much for the succinct code. It does help a lot.

I'm intrigued by the line:

Code:
Dict.CompareMode = vbTextCompare

Does this work in a similar manner to, perhaps, the Worksheet Function =ABS() in that it 'strips' out any extraneous information to get just the 'value' of the underlying item?

Cheers

pvr928
 
Upvote 0
This is basics if it!!!!
vbTextCompare doesn't take Case into account. IE: "HELLO WORLD!" is the same as "hello world!", 1 uppercase, 1 lowercase. vbBinaryCompare is case sensitive, meaning "HELLO WORLD!" and "hello world!" are different.
 
Upvote 0
Thanks very much for explaining this to me MickG. Each time I use a Dictionary I think I've got it sorted and then when next I use one it seems to take a long time to get it right.

Thanks to your example above, I've got it working now.

Cheers

pvr928
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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