Looking up Dictionary (Dict) returns bank result?

MrGadget2000

New Member
Joined
Sep 29, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use a dictionary (new to these) and it appears to record the data correctly, however when I try to get the item for a particular key I get a blank result.

To show, I have the following in A1:C3;



ApplesRed
5​
BananasYellow
2​
OrangesOrange
3​


And the code;

VBA Code:
Sub testdict()
    Dim lastrow As Long
    Dim cnt As Long, fruit As String
    Dim dict As New Scripting.Dictionary

    lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    
    'Set dictionary Items
    For n = 1 To lastrow
        dict.Add Key:=Range("A" & n), Item:=Range("C" & n)
    Next n
    
    'Print Debug Items to immediate - this works;
        'Apples 5
        'Bananas 2
        'Oranges 3
    For i = 0 To dict.Count - 1
        Debug.Print dict.Keys(i), ; dict.Items(i)
    Next i
    
    fruit = "Bananas"
    cnt = dict(Bananas)
    
    Debug.Print "Dict="; dict("Bananas")         ' is blank result
    Debug.Print "Dict 'fruit'="; dict(fruit)     ' is blank result
    Debug.Print "Dict CStr'fruit'="; dict(CStr(fruit)) ' is blank result
    Debug.Print "Dict="; CStr(dict("Bananas"))   ' is blank result
    Debug.Print "cnt=", cnt                      ' is 0
    Debug.Print "cnt (CStr)=", CStr(cnt)         ' is 0

End Sub

I was trying the same code with the addition of the CStr as another post indicated this as a potential solution, however it makes no difference to the outcome.
No matter what I try, I don't seem to be able to get the qty of a fruit.

I'm sure it's a simple resolution but by use of Dictionaries is just starting.

Cheers
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
"If" should be used to check if dict key is existed, also using .value:
VBA Code:
For n = 1 To lastrow
        If Not dict.exists(Range("A" & n).Value) Then
            dict.Add Range("A" & n).Value, Range("C" & n).Value
        End If
    Next
 
Upvote 0
Solution
"If" should be used to check if dict key is existed, also using .value:
VBA Code:
For n = 1 To lastrow
        If Not dict.exists(Range("A" & n).Value) Then
            dict.Add Range("A" & n).Value, Range("C" & n).Value
        End If
    Next
Ahh - the .value works! Knew it would be obvious to someone who knows - thank you!

I did note that I should check if it exists, but wondered if that was really necessary? In short, I will only add to the dictionary at the start, then will make no other additions or changes to it from there, only reading from it as required. I guess it's just good code practice at least unless there is any other obvious reasoning.

Again - thanks - went through a dozen or so mages and missed this.
 
Upvote 0
it's indeed a good practice to check if the key already exists, otherwis you run into errors.

Remark : a dictionary is case sensitive but you can use dict.CompareMode = vbTextCompare
 
Upvote 0
By change, your items are not dupplicate, then dictionary works!
If you want to collect them all (regardless dupplicate), using a collection, or an array.
If you want to store unique items, use dictionary, with "not dict.exists" to check if it dupplicate.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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