Vba 脚本字典功能

Vba 脚本字典功能,vba,dictionary,scripting,Vba,Dictionary,Scripting,我目前正在尝试使用脚本字典对象执行一个函数。但是,它似乎不起作用。我不熟悉脚本字典对象。假设函数检查单元格中的数字是1、9、17还是25。如果是1、9、17或25,则返回“True”,否则返回“False”。尽管有这些数字,函数只返回False Function checkNuminList(r As Integer) Dim myList As Object Set myList = CreateObject("Scripting.Dictionary") myList.Add Key:="N

我目前正在尝试使用脚本字典对象执行一个函数。但是,它似乎不起作用。我不熟悉脚本字典对象。假设函数检查单元格中的数字是1、9、17还是25。如果是1、9、17或25,则返回“True”,否则返回“False”。尽管有这些数字,函数只返回False

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
myList.Exists(r)
其中r表示您的密钥

例如,
Dic.存在(密钥)

由于您的键:=“Num1”等,因此它将始终返回false

假设r指的是该项,一种方法是在字典中循环检查该项是否等于所需的值

例如

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