在Excel VBA中使用字典对象 [英] Using Dictionary Object in Excel VBA
问题描述
我正在努力使我的Dictionary对象工作,以返回所选参考号的经济细节。
例如我有以下参考nos和相应的值,但是,不知道我是否可以使用Dictionary对象来实现这一点,另外,建议将非常感谢。
参考号金额价格年
IB1232 1000 1.23 2011
IB1231 1000 3.23 2011
IB1233 1000 3.43 2011
IB1234 1000 3.43 2011
我以为可以通过形成Key来实现和价值参考及其相应的细节,但未能实现。
我不知道你是什么在VBA中重新引用为Dictionary,因为具有所述功能的数据结构在VBA中被称为 Collection
(但也可能是编码您自己的Ditionary,在这种情况下,我们需要代码为了能够帮助你)。
如果我正确地找到你的例子,你想通过IB1232键访问,例如{1000,1.23,2011} 。你可以通过创建一个这样收集的集合来轻松地做到这一点:
Dim coll as new Collection
Dim data as新集合
data.Add 1000
data.Add 1.23
data.Add 2011
coll.Add数据,IB1232
要访问您的数据,只需通过密钥获取所需的记录(集合)
pre>
Debug.Print coll.Item(IB1232)(1)'Prints 1000
Debug.Print coll.Item(IB1232)(2)'打印1.23
Debug.Print coll.Item(IB1232)(3)'Prints 2010
您还可以使用数组数组
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, however, not sure if I can achieve this using Dictionary object , and alternative, suggestion would be highly appreciated.
Ref No Amount Price Year
IB1232 1000 1.23 2011
IB1231 1000 3.23 2011
IB1233 1000 3.43 2011
IB1234 1000 3.43 2011
I thought would be able to achieve by forming Key and Value for reference and their corresponding details, but not been able to achieve ..
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
这篇关于在Excel VBA中使用字典对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!