数组 – 将数组传递给Dictionary.keys的vba

我有一个字符串MyStrArray数组,我有一个字典对象MyDict

如何在没有循环的情况下将MyStrArray传递给MyDict.keys?可能吗?

如果我尝试MyDict.keys = MyStrArray我得到一个错误“对象不支持此方法”和键边界是MyDict.Keys(0到-1)我知道这意味着什么,我知道我可以循环数组到填写字典.就这样(简单的例子)

on error resume next 
    for i= Lbound(MyStrArray) To Ubound(MyStrArray)
    MyDict.add key:=MyStrArray(i), item:=whatever
    next i

所以,有没有办法解决?

最佳答案 直接的答案是“不”.

以下是为了防止您考虑帮助器以避免在主代码中循环

Option Explicit

Sub main()

Dim myDict As Dictionary

Dim myArr As Variant
myArr = Array("a", "b", "b")

Set myDict = GetDict(myArr)

End Sub


Function GetDict(keysArray As Variant) As Scripting.Dictionary
Dim dict As New Scripting.Dictionary
Dim i As Long
For i = LBound(keysArray) To UBound(keysArray)
    dict.Add Key:=keysArray(i), Item:=i
Next i
Set GetDict = dict
End Function
点赞