Search code examples
vbadictionarydata-structuresvb6

Does VBA have Dictionary Structure?


Does VBA have dictionary structure? Like key<>value array?


Solution

  • Yes.

    Set a reference to MS Scripting runtime ('Microsoft Scripting Runtime'). As per @regjo's comment, go to Tools->References and tick the box for 'Microsoft Scripting Runtime'.

    References Window

    Create a dictionary instance using the code below:

    Set dict = CreateObject("Scripting.Dictionary")
    

    or

    Dim dict As New Scripting.Dictionary 
    

    Example of use:

    If Not dict.Exists(key) Then 
        dict.Add key, value
    End If 
    

    Don't forget to set the dictionary to Nothing when you have finished using it.

    Set dict = Nothing