如何将新对象/类型和类型添加到现有Collection

时间:2013-06-12 11:32:04

标签: excel class vba object collections

我有两个问题。

  1. 我想在新子对象中添加一个新的对象类型(不确定是哪个)到现有的集合中。所以在另一个子目录中,我想将psecs.pNom添加到此集合(证券)。

  2. 我想在这个现有的集合(证券)中添加更多行(secId)。再次在另一个子。

  3. 怎么做?

    谢谢,Amir

    Sub testclass()
    
    rijaantal_LenDump = Application.CountA(Sheets("Len_Dump").Range("A:A"))
    kolomaantal_LenDump = Application.CountA(Sheets("Len_Dump").Range("1:1"))
    
    Sheets("Len_Dump").Select
    positions = Sheets("Len_Dump").Range(Cells(1, 1), Cells(rijaantal_LenDump, kolomaantal_LenDump))
    
    kolomSecID = 8
    
    
    Set securities = New Collection
    
    For i = 1 To rijaantal_LenDump
    Set psecs = New CMpos
    psecs.secId = CStr(positions(i, 8))
    psecs.L4 = CStr(positions(i, 4))
    If Not Exists(securities, psecs.secId) Then securities.Add psecs, psecs.secId
    Next i
    
    Debug.Print securities.Count
    
    End Sub
    

1 个答案:

答案 0 :(得分:0)

将集合ByRef传递给另一个程序。

Public Sub AddAnotherObject(ByRef colSecurities As Collection)

    Dim objOther As SomeOtherObject

    Set objOther = New SomeOtherObject

    colSecurities.Add objOther, CStr(objOther.ID)

End Sub

当您返回调用过程时,集合的计数将高一个,对象将在集合中。 ByRef的反面是ByVal。一旦程序结束,对ByVal传递的变量的更改就会丢失。