VBA 将类添加到集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331106/
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 13:27:52  来源:igfitidea点击:

VBA Adding a class to a collection

vbacollections

提问by Logan

I have a class module called Holding. In it are several public variables. My code is this:

我有一个名为Holding 的类模块。其中有几个公共变量。我的代码是这样的:

Dim holdings as Collection
Dim h as Holding

Set holdings = new Collection

For i = 1 to last
    Set h = new Holding

    h.x = y
    '... etc

    holdings.Add(h)
Next i

This gives me error "object doesnt support this property or method" on the holdings.Add(h)line, but everywhere I look, it gives this exact example of how to achieve this. What am I missing?

这给了我错误“对象不支持此属性或方法” holdings.Add(h),但无论我在哪里看,它都给出了如何实现这一点的确切示例。我错过了什么?

回答by GSerg

Remove the parentheses.

去掉括号。

holdings.Add h

Otherwise you are trying to add to the collection the value of the default property of your Holdinginstance, and it doesn't have a default property.

否则,您正在尝试将Holding实例的默认属性的值添加到集合中,但它没有默认属性。