Answers for "vba array to dictionary"

VBA
0

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
Posted by: Guest on February-13-2021

Code answers related to "VBA"

Browse Popular Code Answers by Language