Create dictionary of lists in vba

后端 未结 3 1567
野趣味
野趣味 2020-12-15 07:07

I have worked in Python earlier where it is really smooth to have a dictionary of lists (i.e. one key corresponds to a list of stuff). I am struggling to achieve the same in

3条回答
  •  醉酒成梦
    2020-12-15 07:54

    Arrays in VBA are more or less like everywhere else with various peculiarities:

    • Redimensioning an array is possible (although not required).
    • Most of the array properties (e.g., Sheets array in a Workbook) are 1-based. Although, as rightly pointed out by @TimWilliams, the user-defined arrays are actually 0-based. The array below defines a string array with a length of 11 (10 indicates the upper position).

    Other than that and the peculiarities regarding notations, you shouldn't find any problem to deal with VBA arrays.

    Dim stringArray(10) As String
    stringArray(1) = "first val"
    stringArray(2) = "second val"
    'etc.
    

    Regarding what you are requesting, you can create a dictionary in VBA and include a list on it (or the VBA equivalent: Collection), here you have a sample code:

    Set dict = CreateObject("Scripting.Dictionary")
    Set coll = New Collection
    coll.Add ("coll1")
    coll.Add ("coll2")
    coll.Add ("coll3")
    If Not dict.Exists("dict1") Then
        dict.Add "dict1", coll
    End If
    
    Dim curVal As String: curVal = dict("dict1")(3) '-> "coll3"
    
    Set dict = Nothing 
    

提交回复
热议问题