Using Dictionary Object in Excel VBA

后端 未结 3 637
星月不相逢
星月不相逢 2021-01-06 01:33

I am struggling to make my Dictionary object work to return economic details of selected Reference number.

e.g. I have below reference nos and corresponding values,

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 02:17

    I don't know what you're referring to as Dictionary in VBA, as the data structure with the said functionality is called Collection in VBA (but maybe you coded your own Ditionary, in that case we need the code in order to be able to help you).

    If I get your example right, you want to access e.g {1000,1.23,2011} via the key "IB1232". You can do this easily by creating a Collection of Collections like this:

    Dim coll as new Collection
    Dim data as new Collection
    
    data.Add 1000
    data.Add 1.23
    data.Add 2011
    
    coll.Add data, "IB1232"
    

    To access your data just get the desired record (Collection) via the key

    Debug.Print coll.Item("IB1232")(1) 'Prints 1000
    Debug.Print coll.Item("IB1232")(2) 'Prints 1.23
    Debug.Print coll.Item("IB1232")(3) 'Prints 2010
    

    You can also use an array of Variants for the data

提交回复
热议问题