VBA函数返回集合
我试图从函数返回类型VBA函数返回集合,vba,excel,Vba,Excel,我试图从函数返回类型集合,但得到错误: 编译错误: 财产的无效使用 下面是我为测试函数而创建的代码。我知道集合正在正确填充,因为我已经测试了集合的运行并输出了所有值;这一切都如期而至 问题: 为什么会发生这种错误?最好的解决方法是什么 Sub testingFunc() Dim ErrorCodes As Collection Set ErrorCodes = New Collection Set InitializeErrorCodes = ErrorCodes '<
集合
,但得到错误:
编译错误:
财产的无效使用
下面是我为测试函数而创建的代码。我知道集合正在正确填充,因为我已经测试了集合的运行并输出了所有值;这一切都如期而至
问题:
为什么会发生这种错误?最好的解决方法是什么
Sub testingFunc()
Dim ErrorCodes As Collection
Set ErrorCodes = New Collection
Set InitializeErrorCodes = ErrorCodes '<--- Where my error is occurring when trying to retrieve the collection
End Sub
下面是与上面一起添加到集合中的函数
Private Sub AddToErrorCollection(Col As Collection, eName As String, eDescription As String)
Dim NewErrorCode As cErrorCodes
Set NewErrorCode = New cErrorCodes
NewErrorCode.name = eName
NewErrorCode.description = eDescription
Col.Add NewErrorCode
End Sub
InitializeErrorCodes
是一个函数,不是属性,但您正试图将其用作属性。
正确的调用应该是Set ErrorCodes=InitializeErrorCodes
由于InitializeErrorCodes执行集合初始化,您应该在
testingFunc
中删除Set ErrorCodes=New collection
,InitializeErrorCodes是一个函数,而不是属性,您正试图将其用作属性。正确的调用应该是Set ErrorCodes=InitializeErrorCodes
。由于InitializeErrorCodes执行集合初始化,您应该在testingFunc
@VincentG My oh My中删除Set ErrorCodes=New collection
。。。不知道我怎么会错过这样一个新手的错误。。。休息了两周后,显然有点生锈了。谢谢你帮我发现了!你能把答案贴出来让我标记一下吗?
Private Sub AddToErrorCollection(Col As Collection, eName As String, eDescription As String)
Dim NewErrorCode As cErrorCodes
Set NewErrorCode = New cErrorCodes
NewErrorCode.name = eName
NewErrorCode.description = eDescription
Col.Add NewErrorCode
End Sub