如何在 VBA 中准备 Collection 对象
下面的代码是在 VB.net 中,我怎样才能用 vba
Option Strict On
Imports System.Collections
Public Class Collect
Public Shared Sub Main()
Dim sta As New Collection
sta.Add("New York", "NY")
sta.Add("Michigan", "MI")
sta.Add("New Jersey", "NJ")
sta.Add("Massachusetts", "MA")
End Sub
End Class
准备好 vba 集合后对象,我想通过以下方式检索它key,假设我想要键“New York”的值。它应该返回纽约。
Below code is in VB.net, how can i do the same thing with vba
Option Strict On
Imports System.Collections
Public Class Collect
Public Shared Sub Main()
Dim sta As New Collection
sta.Add("New York", "NY")
sta.Add("Michigan", "MI")
sta.Add("New Jersey", "NJ")
sta.Add("Massachusetts", "MA")
End Sub
End Class
After preparing the vba collection object, i want to retrieve it by key,Suppose take I want value for the Key "New York". It should return NY.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在 VBA 集合中执行此操作(更新:按照您在 vb.net 中布置的顺序,我注意到 Jean 已重新排序您的参数以满足您对集合的需求) ,您可以使用字典来做到这一点,请参见下文
字典比集合更高效,用途更广泛,因此我建议这样做
有用的阅读:Patrick Matthews 在 VBA 中使用字典类(以及集合和字典有何不同) <一href="http://www.experts-exchange.com/A_3391.html" rel="nofollow">http://www.experts-exchange.com/A_3391.html
You can't do this in a VBA Collection (update: in the same order as you have laid out in vb.net, I note Jean has re-ordered your arguments to meet your needs a collection), you can do it with a Dictionary, see below
Dictionaries are more efficient and more versatile than Collections, so I would recommend going that way
Useful reading: Patrick Matthews Using the Dictionary Class in VBA (and how Collections and Dictionaries differ) http://www.experts-exchange.com/A_3391.html
以下是如何向 Collection 对象添加项目并通过键检索它们:
如您所见,参数顺序与 .NET 中的顺序相反。为了避免任何混淆,您可以使用命名参数,例如
Here's how to add items to a Collection object and retrieve them by key:
As you can see, the argument order is the reverse of that in .NET. To avoid any mix-up, you could use named arguments instead, e.g.
OtherWise 是创建一个您的类型,例如
[]´s
OtherWise is create a your Type, for example
[]´s