我有以下代码:
Dim dicMyHash As Dictionary
Dim rngMyRange As Range
' A1 is empty - although the outcome is the same in any case
Set rngMyRange = Range("A1")
Set dicMyHash = New Dictionary
dicMyHash.Add Key:=rngMyRange(1), Item:=0
Debug.Print dicMyHash.Exists(rngMyRange(1).Value) ' returns False
Debug.Print rngMyRange(1) = rngMyRange(1).Value ' returns True
这种行为有些出乎意料。后台是否正在进行某种类型转换?rngMyRange(1).Value
属性返回 a variant
,而rngMyRange(1)
is rngMyRange.item(1)
,它是 a range
。但是,强制转换rngMyRange(1)
为Variant
给出相同的结果..
此外,添加键是按值(因此副本rngMyRange(1)
作为键传递)。但我仍然不明白为什么.Exists
找不到钥匙..
先感谢您!