将自定义类集合添加到另一个自定义类集合

时间:2016-07-20 17:04:21

标签: vba excel-vba collections excel

好的开始,我通读了this。 尽管它没有回答我的具体问题,但它已经接近了。这涉及采用较小的集合并将项目添加到更大的主集合。然后摧毁较小的集合。

我在类模块下有两个定义。

TimeDet

Option Explicit
Public recDate As String
Public recQty As String
Public recDieNo As String
Public recCatID As String
Public recCatName As String
Public recGroupID As String
Public recGroupName As String    

TimeRec

Option Explicit
Private objTimeRec As Collection

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

Private Sub Class_Terminate()
    Set objTimeRec = Nothing
End Sub

Public Property Get NewEnum() As IUnknown
    Set NewEnum = objTimeRec.[_NewEnum]
End Property

Public Sub Add(obj As TimeDet)
    objTimeRec.Add obj
End Sub

Public Sub Remove(Index As Variant)
    objTimeRec.Remove Index
End Sub

Public Property Get Item(Index As Variant) As TimeDet
    Set Item = objTimeRec.Item(Index)
End Property

Property Get Count() As Long
    Count = objTimeRec.Count
End Property

Public Sub Clear()
    Set objTimeRec = New Collection
End Sub

Public Sub FillFromArray(Arr As Variant)
    Dim i As Long, obj As TimeDet
    For i = 1 To UBound(Arr)
        Set obj = New TimeDet
        obj.recDate = Arr(i, 1)
        obj.recQty = Arr(i, 2)
        obj.recDieNo = Arr(i, 3)
        obj.recCatID = Arr(i, 4)
        obj.recCatName = Arr(i, 5)
        obj.recGroupID = Arr(i, 6)
        obj.recGroupName = Arr(i, 7)
        Me.Add obj
    Next

End Sub    

然后在代码中我以这种方式使用它:

Sub Test()
Dim RecSet1 As TimeRec, Record As TimeDet
Dim fSet1 As TimeRec, fRecord As TimeDet
Dim repArray() As Variant
Dim startDT As Date, endDT As Date, dieNo As String

repArray() = Sheet4.Range("A2:G" & Sheet4.Range("A2").End(xlDown).Row)

Set RecSet1 = New TimeRec
Set fSet1 = New TimeRec

RecSet1.FillFromArray (repArray())

startDT = "1-1-2015"
endDT = "1-1-2016"
dieNo = "16185"

For Each Record In RecSet1
    If Record.recDate <= endDT And Record.recDate >= startDT And Record.recDieNo = dieNo Then
        fSet1.Add (Record)
    End If
Next
End Sub

当我尝试将Record对象添加到fSet1对象时,我收到错误。 &#34;对象不支持此方法或属性&#34;

Record对象是Type TimeDet,你可以在类模块中看到我的Add方法期望类型为TimeDet。

要么我错过了一些非常简单的东西并且有遮挡,或者这是一个更大的问题。

该数组大致有200,000条记录。我正在尝试创建一个较小的过滤数据子集。也许我正在以错误的方式接近这一点。

2 个答案:

答案 0 :(得分:1)

您的错误不是Add,而是For Each

您很可能复制了TimeRec类。在VBA中,您无法在VBE(VBA IDE)中创建可枚举的类。有一种不同的创建Enumerable类的方法。

打开记事本,复制所有类代码,然后将此属性添加到NewEnum属性Attribute NewEnum.VB_UserMemId = -4

然后导入课程。

这总是隐藏在VBA代码中,但可以在文本编辑器中看到。 同时将此属性添加到Item属性,它将使其成为默认值并允许类似ClassName(1)的语法

Attribute Item.VB_UserMemId = 0

因此,您在文本编辑器/记事本中的代码应为:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private objTimeRec As Collection

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

Private Sub Class_Terminate()
    Set objTimeRec = Nothing
End Sub

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
    Set NewEnum = objTimeRec.[_NewEnum]
End Property

Public Sub Add(obj As Class2)
    objTimeRec.Add obj
End Sub

Public Sub Remove(Index As Variant)
    objTimeRec.Remove Index
End Sub

Public Property Get Item(Index As Variant) As Class2
Attribute Item.VB_UserMemId = 0
    Set Item = objTimeRec.Item(Index)
End Property

Property Get Count() As Long
    Count = objTimeRec.Count
End Property

Public Sub Clear()
    Set objTimeRec = New Collection
End Sub

答案 1 :(得分:0)

这个特殊问题的答案是从我的Add方法中删除括号。话虽这么说,隐藏的属性信息是非常好的信息,并且在我发现删除括号修复它之后可能会导致问题。