Excel vba:错误的参数数量或无效的属性赋值集合添加

Ans*_*Ans 9 excel vba excel-vba

我一直在接受

错误的参数个数或无效的属性赋值集合

错误很长一段时间了,但无法弄清楚出了什么问题.我有一个类和一个Collection内部类和一个Sub为该集合添加值.

Private sumLosses As Collection

Private Sub Class_Initialize() 
    Set sumLosses = New Collection
End Sub

Public Property Get getSumLosses()
    getSumLosses = sumLosses
End Property
Run Code Online (Sandbox Code Playgroud)

内部主模块:

For Each clientCopy In clientsColl
        clientCopy.getSumLosses.Add 200  'error
        clientCopy.getSumLosses.Add (200) 'error
Next
Run Code Online (Sandbox Code Playgroud)

为什么会失败,如何将项添加到类的集合中?

Pᴇʜ*_*Pᴇʜ 8

sumLosses是类型的Collection因此它是一个对象,必须是Set另一个变量/函数.

使用Set它应该工作:

Public Property Get getSumLosses() As Collection
    Set getSumLosses = sumLosses
End Property
Run Code Online (Sandbox Code Playgroud)

定义属性As Collection也许并不是一个坏主意(但这不会导致错误).


ash*_*awg 5

您需要声明Public Property Get getSumLosses()为a Collection并使用Set:

Private sumLosses As Collection

Private Sub Class_Initialize() 
    Set sumLosses = New Collection
End Sub

Public Property Get getSumLosses() as Collection
    Set getSumLosses = sumLosses
End Property
Run Code Online (Sandbox Code Playgroud)

:)