Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to lookup VBA Dictionary key

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
GB
I'm familiar with defining a Dictionary object and storing pairs of values, e.g.
ProductCode234, Bread
ProductCode546, Milk
ProductCode812, Tea

I know that if I want the value of product code 234 I use dictionary.item("ProductCode234") to retrieve the value "Bread".

But how can I lookup "Bread" to retrieve the product code? Is this possible without using a for..each loop?

Thanks,
May
 
The key has to be unique, item text - no. So there is no direct way to get the key for given item. For 1:1 relation (key - value) you could consider second dictionary (or array for code numbers within reasonable range).

combo
 
May,
If you are using the Dictionary object of VB Scripting, I was reading ( and it is refering to the key value as:
Code:
'This is how you iterate through the dictionary object and 'print all the key and item pairs:

Dim key As Variant
For Each key In dic.Keys
Debug.Print CStr(key) & " - " & dic.Item(key)
Next


This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top