Rachel Chia
Rachel Chia

Reputation: 259

Scripting Dictionary Function

I am currently trying to do a function using Scripting Dictionary Object.However, it doesn't seems to work.I am new to Scripting Dictionary Object. The function is suppose to check if the number in the cell is 1,9,17 or 25. If it is 1,9,17 or 25, it will return 'True' else it will return 'False'. The function only returns False despite having those numbers.

Function checkNuminList(r As Integer)
Dim myList As Object
Set myList = CreateObject("Scripting.Dictionary")
myList.Add Key:="Num1", Item:="1"
myList.Add Key:="Num2", Item:="9"
myList.Add Key:="Num3", Item:="17"
myList.Add Key:="Num4", Item:="25"
If myList.Exists(r) Then
checkNuminList = "True"
Else
checkNuminList = "False"
End If
End Function

Upvotes: 1

Views: 637

Answers (1)

Keenlearner
Keenlearner

Reputation: 704

myList.Exists(r) where r refers to your key

Eg. Dic.Exists(Key)

Since your Key:="Num1" etc. hence it will return false all the way.

Assuming r is referring to the item, one way would to be loop through the dictionary to check whether the item is equal to the value you desire

Eg.

Dim key As Variant
For Each key In Dic.Keys
    If Dic.Item(key) = r Then
        checkNuminList = "True"
    Else
        checkNuminList = "False"
    End If
Next key

Upvotes: 3

Related Questions