-
天涯尽头无女友
是。设置对MS脚本运行时(“Microsoft脚本运行时”)的引用。根据@Regjo的评论,转到Tools->Reference并勾选“MicrosoftScriptingRuntime”框。使用下面的代码创建一个字典实例:Set dict = CreateObject("Scripting.Dictionary")或Dim dict As New Scripting.Dictionary使用实例:If Not dict.Exists(key) Then
dict.Add key, valueEnd If别忘了把字典设为Nothing当你用完它的时候。Set dict = Nothing
-
至尊宝的传说
VBA有一个集合对象: Dim c As Collection Set c = New Collection
c.Add "Data1", "Key1"
c.Add "Data2", "Key2"
c.Add "Data3", "Key3"
'Insert data via key into cell A1
Range("A1").Value = c.Item("Key2")这个Collection对象使用散列执行基于键的查找,因此它非常快速。您可以使用Contains()函数以检查特定集合是否包含键:Public Function Contains(col As Collection, key As Variant) As Boolean
On Error Resume Next
col(key) ' Just try it. If it fails, Err.Number will be nonzero.
Contains = (Err.Number = 0)
Err.ClearEnd Function
-
长风秋雁
VBA没有字典的内部实现,但是从VBA您仍然可以使用MS脚本运行时库中的字典对象。Dim dSet d = CreateObject("Scripting.Dictionary")d.Add "a", "aaa"d.Add "b", "bbb"d.Add "c", "ccc"If d.Exists("c") Then
MsgBox d("c")End If