Vba passes array to Dictionary.keys

I have an array of strings MyStrArrayand I have a dictionary objectMyDict

How to transfer MyStrArrayin MyDict.keys without loop ? Is it possible?

if I try to execute MyDict.keys = MyStrArrayI get the error message "the object does not support this method", and the key boundaries MyDict.Keys(0 to -1)I know what this means, and I know that I can iterate over the array to fill the dict. as simple (simple example)

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

So is there a way around this?

+4
source share
1 answer

The direct answer is NO.

The following, just in case, if you consider a helper, to avoid a loop in the main code

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
+1
source

All Articles