Excel VBA - 在父类中创建集合类

时间:2015-01-20 18:51:57

标签: excel-vba vba excel

我一直在尝试创建一个类,它是(另一个类)对象的集合,并且在父类中。我在这里查看了几个问题,但无法使其正常运行。因此,如果任何人都可以使用我的参数发布一个简短的代码,我将非常感激。

我的父类是Sample。它应该包含一个SampleFields集合,它应该包含SampleField类中的对象。 SampleField对象只有一个Name属性,它取自单元格A1到D1。应该可以在SampleFields集合中添加和删除项目,并修改SampleField对象的Name属性。 SampleFields集合在Sample类初始化时获取其对象。

我需要像这样访问它 - Sample.SampleFields(1).Name

我认为发布我的尝试是没用的,但现在是:

Sub test()

Dim a As New Sample, i As Variant

a.GetFields

For Each i In a.SampleFields
    Debug.Print i.Name
Next

End Sub

示例类:

Private pFields As New SampleFields

Public Property Get SampleFields() As SampleFields
    Set SampleFields= pFields
End Property

Public Property Set SampleFields(ByVal value As SampleFields)
    Set pFields = value
End Property

Private Sub Initialize_Class()
    Set pFields = New SampleFields
End Sub


Public Sub GetFields()

Dim rngHeaders As Range, rngCell As Range
Set rngHeaders = Range("A1").CurrentRegion.Rows(1)

For Each rngCell In rngHeaders.Cells
    Dim newField As SampleField
    newField.Name = rngCell.Value2
    Me.Fields.AddNewField (newField)   'crashes here with Method or data member not found
Next

End Sub

SampleFields类:

Private pFields As New Collection

Public Sub AddNewField(FieldName As SampleField)
    Me.AddNewField (FieldName)
End Sub

SampleField类:

Private pName As String

Public Property Let Name(value As String)
    pName = value
End Property

Public Property Get Name() As String
    Name = pName
End Property

谢谢!

2 个答案:

答案 0 :(得分:2)

很老的帖子,但至少让我回答一下: 在示例类中,有一个Collection。您可以忘记SampleFields类,但不需要它。

然后你只需要有一个SampleField类传递给这个SampleClass方法" AddField"您用来增加集合的大小。

示例类应如下所示:

Private p_SampleFields as Collection
Private p_SampleField as SampleField

'Initialize this class with it's collection:
Private Sub Class_Initialize()
    Set p_SampleFields = New Collection
End Sub

'Allow for adding SampleFields:
Public Sub AddField(field as SampleField)
    Set p_SampleField = field
    p_sampleFields.add field
End Sub

'Expose the collection:
Public Property Get SampleFields() as Collection
    Set SampleFields = p_SampleFields
End Property

在常规模块中,您可以使用以下内容:

Sub Test()
    Dim sField as SampleField
    Dim sClass as SampleClass

    Set sField = New SampleField
    Set sClass = New SampleClass

    sField.Name = "SomeName"
    sClass.AddField sField 'This adds it to the collection

    'Access as per requirement:
    msgbox sClass.SampleFields(1).Name 'Pop-up saying "SomeName"
End Sub

答案 1 :(得分:-1)

在Rik的答案上有一点变化,我们可以使用公共集合,而无需使用AddField和Get方法:

Class SampleClass:

Public SampleFields As Collection

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

然后在您的模块中使用它:

Sub Test()
    Dim sField as AnyOtherClass
    Dim sClass as SampleClass

    Set sField = New AnyOtherClass
    Set sClass = New SampleClass

    sField.Name = "SomeName"
    sClass.SampleFields.add sField 'This adds it to the collection

    'Access as per requirement:
    msgbox sClass.SampleFields(1).Name 'Pop-up saying "SomeName"
End Sub