pass array to dictionary vba code example

Example: vba array to dictionary

' Converts an array to a dictionary (key = value, unique keys)
Function ArrayToDictionary(pArray As Variant) As Dictionary
    Set ArrayToDictionary = New Dictionary
    Dim index As Long
    For index = LBound(pArray) To UBound(pArray)
        If Not ArrayToDictionary.Exists(pArray(index)) Then
            ArrayToDictionary.add key:=pArray(index), item:=pArray(index)
        End If
    Next index
End Function
' ------------------------------------------------------------------------
Sub TestMe()
    Dim myDict As Dictionary, myArray As Variant
    myArray = Array("a", "b", "c")
    Set myDict = ArrayToDictionary(myArray)
    Debug.Print myDict("a")			' a
End Sub

Tags:

Vb Example