VBA有字典结构吗?比如key<>value array?


当前回答

VBA可以使用Scripting.Runtime的字典结构。

它的实现实际上是一个很奇特的——只要执行myDict(x) = y,它就会检查字典中是否有键x,如果没有,它甚至会创建一个键x。如果它在那里,它就利用它。

而且它不会对这个“幕后”执行的额外步骤“大喊大叫”或“抱怨”。当然,您可以显式地使用Dictionary.Exists(key)检查键是否存在。因此,这5行:

If myDict.exists("B") Then
    myDict("B") = myDict("B") + i * 3
Else
    myDict.Add "B", i * 3
End If

myDict("B") = myDict("B") + i * 3。看看吧:

Sub TestMe()

    Dim myDict As Object, i As Long, myKey As Variant
    Set myDict = CreateObject("Scripting.Dictionary")
    
    For i = 1 To 3
        Debug.Print myDict.Exists("A")
        myDict("A") = myDict("A") + i
        myDict("B") = myDict("B") + 5
    Next i
    
    For Each myKey In myDict.keys
        Debug.Print myKey; myDict(myKey)
    Next myKey

End Sub

其他回答

VBA可以使用Scripting.Runtime的字典结构。

它的实现实际上是一个很奇特的——只要执行myDict(x) = y,它就会检查字典中是否有键x,如果没有,它甚至会创建一个键x。如果它在那里,它就利用它。

而且它不会对这个“幕后”执行的额外步骤“大喊大叫”或“抱怨”。当然,您可以显式地使用Dictionary.Exists(key)检查键是否存在。因此,这5行:

If myDict.exists("B") Then
    myDict("B") = myDict("B") + i * 3
Else
    myDict.Add "B", i * 3
End If

myDict("B") = myDict("B") + i * 3。看看吧:

Sub TestMe()

    Dim myDict As Object, i As Long, myKey As Variant
    Set myDict = CreateObject("Scripting.Dictionary")
    
    For i = 1 To 3
        Debug.Print myDict.Exists("A")
        myDict("A") = myDict("A") + i
        myDict("B") = myDict("B") + 5
    Next i
    
    For Each myKey In myDict.keys
        Debug.Print myKey; myDict(myKey)
    Next myKey

End Sub

一个额外的字典示例,用于包含出现频率。

在循环外:

Dim dict As New Scripting.dictionary
Dim MyVar as String

在循环中:

'dictionary
If dict.Exists(MyVar) Then
    dict.Item(MyVar) = dict.Item(MyVar) + 1 'increment
Else
    dict.Item(MyVar) = 1 'set as 1st occurence
End If

检查频率:

Dim i As Integer
For i = 0 To dict.Count - 1 ' lower index 0 (instead of 1)
    Debug.Print dict.Items(i) & " " & dict.Keys(i)
Next i

所有其他人都已经提到了Dictionary类的scripting.runtime版本的使用。如果您无法使用此DLL,您也可以使用此版本,只需将其添加到代码中。

https://github.com/VBA-tools/VBA-Dictionary/blob/master/Dictionary.cls

它与微软的版本完全相同。

是的。适用于VB6, VBA (Excel), VB。网

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.Clear
End Function

编辑2015年6月24日:短包含()感谢@TWiStErRob。

2015年9月25日编辑:感谢@scipilot,添加了Err.Clear()。