Excel 如何向现有集合添加新对象/类型和类型

Excel 如何向现有集合添加新对象/类型和类型,excel,class,vba,object,collections,Excel,Class,Vba,Object,Collections,我有两个问题 我想将新sub中的新对象类型(不确定是哪种类型)添加到基础sub中创建的现有集合中。因此,在其他sub中,我想将psecs.pNom添加到此集合(证券) 我想在现有集合(证券)中添加更多行(secId)。又是在另一艘潜艇上 如何做到这一点 谢谢,阿米尔 Sub testclass() rijaantal_LenDump = Application.CountA(Sheets("Len_Dump").Range("A:A")) kolomaantal_LenDump = Appli

我有两个问题

  • 我想将新sub中的新对象类型(不确定是哪种类型)添加到基础sub中创建的现有集合中。因此,在其他sub中,我想将psecs.pNom添加到此集合(证券)

  • 我想在现有集合(证券)中添加更多行(secId)。又是在另一艘潜艇上

  • 如何做到这一点

    谢谢,阿米尔

    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
    

    将集合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。一旦过程结束,对通过VAL传递的变量的更改将丢失