Mit*_*eat 332
是.
设置对MS Scripting运行时的引用('Microsoft Scripting Runtime').根据@ regjo的评论,转到Tools-> References并勾选"Microsoft Scripting Runtime"框.
使用以下代码创建字典实例:
Set dict = CreateObject("Scripting.Dictionary")
Run Code Online (Sandbox Code Playgroud)
要么
Dim dict As New Scripting.Dictionary
Run Code Online (Sandbox Code Playgroud)
使用示例:
If Not dict.Exists(key) Then
dict.Add key, value
End If
Run Code Online (Sandbox Code Playgroud)
Nothing完成使用后,不要忘记将字典设置为.
Set dict = Nothing
Run Code Online (Sandbox Code Playgroud)
Cal*_*ngh 172
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")
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
编辑2015年6月24日:Contains()感谢@TWiStErRob.
编辑2015年9月25日:Err.Clear()感谢@scipilot.
Jar*_*rmo 43
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
Run Code Online (Sandbox Code Playgroud)
Joh*_*n M 29
一个附加的字典示例,可用于包含出现的频率.
循环之外:
Dim dict As New Scripting.dictionary
Dim MyVar as String
Run Code Online (Sandbox Code Playgroud)
在一个循环中:
'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
Run Code Online (Sandbox Code Playgroud)
检查频率:
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
Run Code Online (Sandbox Code Playgroud)
建立关cjrh的回答中,我们可以构建一个包含不需要标签(我不喜欢使用标签)功能.
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
Run Code Online (Sandbox Code Playgroud)
对于我的一个项目,我编写了一组辅助函数来使Collection行为更像是一个Dictionary.它仍然允许递归集合.您会注意到Key始终是第一位的,因为它是强制性的,在我的实现中更有意义.我也只使用了String钥匙.如果你愿意,你可以改回来.
我将其重命名为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
Run Code Online (Sandbox Code Playgroud)
这些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
Run Code Online (Sandbox Code Playgroud)
这篇帖子的原因......
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
Run Code Online (Sandbox Code Playgroud)
如果它不存在则不抛出.只是确保它被删除.
Private Sub cRemove(ByRef Col As Collection, Key As String)
If cHas(Col, Key) Then Col.Remove Key
End Sub
Run Code Online (Sandbox Code Playgroud)
获取一系列密钥.
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
Run Code Online (Sandbox Code Playgroud)
小智 5
如果出于某种原因,您无法或不想在 Excel 中安装其他功能,您也可以使用数组,至少对于简单的问题是这样。作为 WhatIsCapital,您输入国家/地区名称,该函数将返回其首都。
Sub arrays()
Dim WhatIsCapital As String, Country As Array, Capital As Array, Answer As String
WhatIsCapital = "Sweden"
Country = Array("UK", "Sweden", "Germany", "France")
Capital = Array("London", "Stockholm", "Berlin", "Paris")
For i = 0 To 10
If WhatIsCapital = Country(i) Then Answer = Capital(i)
Next i
Debug.Print Answer
End Sub
Run Code Online (Sandbox Code Playgroud)
所有其他人都已经提到了使用 Dictionary 类的 scripting.runtime 版本。如果您无法使用此 DLL,您也可以使用此版本,只需将其添加到您的代码中即可。
https://github.com/VBA-tools/VBA-Dictionary/blob/master/Dictionary.cls
它与 Microsoft 的版本相同。
| 归档时间: |
|
| 查看次数: |
274277 次 |
| 最近记录: |