VBA有字典结构吗?比如key<>value array?
当前回答
基于cjrh的答案,我们可以构建一个不需要标签的Contains函数(我不喜欢使用标签)。
Public Function Contains(Col As Collection, Key As String) As Boolean
Contains = True
On Error Resume Next
err.Clear
Col (Key)
If err.Number <> 0 Then
Contains = False
err.Clear
End If
On Error GoTo 0
End Function
在我的一个项目中,我编写了一组帮助函数,使集合的行为更像字典。它仍然允许递归收集。你会注意到Key总是排在前面,因为它是强制性的,在我的实现中更有意义。我也只使用字符串键。如果你愿意,你可以改回来。
Set
我将其重命名为set,因为它将覆盖旧值。
Private Sub cSet(ByRef Col As Collection, Key As String, Item As Variant)
If (cHas(Col, Key)) Then Col.Remove Key
Col.Add Array(Key, Item), Key
End Sub
Get
err的东西是为对象,因为你会传递对象使用set和变量没有。我觉得你可以检查一下它是不是一个物体,但我时间很紧。
Private Function cGet(ByRef Col As Collection, Key As String) As Variant
If Not cHas(Col, Key) Then Exit Function
On Error Resume Next
err.Clear
Set cGet = Col(Key)(1)
If err.Number = 13 Then
err.Clear
cGet = Col(Key)(1)
End If
On Error GoTo 0
If err.Number <> 0 Then Call err.raise(err.Number, err.Source, err.Description, err.HelpFile, err.HelpContext)
End Function
Has
写这篇文章的原因是…
Public Function cHas(Col As Collection, Key As String) As Boolean
cHas = True
On Error Resume Next
err.Clear
Col (Key)
If err.Number <> 0 Then
cHas = False
err.Clear
End If
On Error GoTo 0
End Function
删除
如果它不存在就不抛出。只要确保它被移除。
Private Sub cRemove(ByRef Col As Collection, Key As String)
If cHas(Col, Key) Then Col.Remove Key
End Sub
Keys
获取一个键数组。
Private Function cKeys(ByRef Col As Collection) As String()
Dim Initialized As Boolean
Dim Keys() As String
For Each Item In Col
If Not Initialized Then
ReDim Preserve Keys(0)
Keys(UBound(Keys)) = Item(0)
Initialized = True
Else
ReDim Preserve Keys(UBound(Keys) + 1)
Keys(UBound(Keys)) = Item(0)
End If
Next Item
cKeys = Keys
End Function
其他回答
脚本运行时字典似乎有一个bug,可能会在高级阶段破坏您的设计。
如果字典值是数组,则不能通过对字典的引用更新数组中包含的元素的值。
VBA没有字典的内部实现,但是在VBA中你仍然可以使用MS Scripting Runtime Library中的字典对象。
Dim d
Set 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
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()。
基于cjrh的答案,我们可以构建一个不需要标签的Contains函数(我不喜欢使用标签)。
Public Function Contains(Col As Collection, Key As String) As Boolean
Contains = True
On Error Resume Next
err.Clear
Col (Key)
If err.Number <> 0 Then
Contains = False
err.Clear
End If
On Error GoTo 0
End Function
在我的一个项目中,我编写了一组帮助函数,使集合的行为更像字典。它仍然允许递归收集。你会注意到Key总是排在前面,因为它是强制性的,在我的实现中更有意义。我也只使用字符串键。如果你愿意,你可以改回来。
Set
我将其重命名为set,因为它将覆盖旧值。
Private Sub cSet(ByRef Col As Collection, Key As String, Item As Variant)
If (cHas(Col, Key)) Then Col.Remove Key
Col.Add Array(Key, Item), Key
End Sub
Get
err的东西是为对象,因为你会传递对象使用set和变量没有。我觉得你可以检查一下它是不是一个物体,但我时间很紧。
Private Function cGet(ByRef Col As Collection, Key As String) As Variant
If Not cHas(Col, Key) Then Exit Function
On Error Resume Next
err.Clear
Set cGet = Col(Key)(1)
If err.Number = 13 Then
err.Clear
cGet = Col(Key)(1)
End If
On Error GoTo 0
If err.Number <> 0 Then Call err.raise(err.Number, err.Source, err.Description, err.HelpFile, err.HelpContext)
End Function
Has
写这篇文章的原因是…
Public Function cHas(Col As Collection, Key As String) As Boolean
cHas = True
On Error Resume Next
err.Clear
Col (Key)
If err.Number <> 0 Then
cHas = False
err.Clear
End If
On Error GoTo 0
End Function
删除
如果它不存在就不抛出。只要确保它被移除。
Private Sub cRemove(ByRef Col As Collection, Key As String)
If cHas(Col, Key) Then Col.Remove Key
End Sub
Keys
获取一个键数组。
Private Function cKeys(ByRef Col As Collection) As String()
Dim Initialized As Boolean
Dim Keys() As String
For Each Item In Col
If Not Initialized Then
ReDim Preserve Keys(0)
Keys(UBound(Keys)) = Item(0)
Initialized = True
Else
ReDim Preserve Keys(UBound(Keys) + 1)
Keys(UBound(Keys)) = Item(0)
End If
Next Item
cKeys = Keys
End Function
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
推荐文章
- 从IEnumerable<KeyValuePair<>>重新创建字典
- 如何获得字典中的键列表?
- 为什么我们使用数组而不是其他数据结构?
- 如何实现一个有序的,默认字典?
- 如何从字典中获得一个随机值?
- 在Java中从HashMap获取键
- 数组与链表
- Visual Basic 6.0与VBA的区别
- 从csv文件创建字典?
- set()是如何实现的?
- 哈希字典?
- 如何将字典列表合并为一个字典?
- .Net数据结构:ArrayList, List, HashTable, Dictionary, SortedList, SortedDictionary—速度,内存,以及何时使用每个?
- 为什么std::map被实现为红黑树?
- 在Python中创建一个初始容量的列表