在 Excel 中使用 VBA 通过索引 # 选择字典项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10757350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:17:23  来源:igfitidea点击:

Selecting Item of Dictionary by Index # using VBA in Excel

vbacollectionsdictionary

提问by Ehudz

I am trying to load a combo box with the last item in a dictionary. I am trying to do something like this ComboBox1.Value = NodeColl.Item(NodeColl.Count)which would work with a collection, but does something strange when using a dictionary instead.

我正在尝试使用字典中的最后一项加载组合框。我正在尝试做这样的事情ComboBox1.Value = NodeColl.Item(NodeColl.Count),它可以与集合一起使用,但是在使用字典时会做一些奇怪的事情。

回答by Alex K.

The behaviour is different because a Dictionary allows numeric keys.

行为是不同的,因为字典允许数字键。

Calling .Itemactually addsan item with the given key so;

调用.Item实际上使用给定的键添加了一个项目;

NodeColl.Item(NodeColl.Count)

Adds a new item with no value & a key corresponding to the count.

添加一个没有值的新项目和一个与计数对应的键。

To access the ordinal item use .Items(which is an array of the items)

要访问序数项使用.Items(这是项的数组)

firstItem = NodeColl.Items(0)
lastItem  = NodeColl.Items(NodeColl.Count - 1)