Collection 对象Collection object

Collection 对象是一组可称为“单元”的有序项。A Collection object is an ordered set of items that can be referred to as a unit.

注解Remarks

使用 Collection 对象,可以非常便捷地将一组相关项引用为一个对象。The Collection object provides a convenient way to refer to a related group of items as a single object. 只有当集合中的项或成员位于集合中时,它们才必须相互关联。The items, or members, in a collection need only be related by the fact that they exist in the collection. 集合成员无需共用相同的数据类型Members of a collection don't have to share the same data type.

集合的创建方式与其他对象相同。A collection can be created the same way other objects are created. 例如:For example:

Dim X As New Collection

在集合创建后,可使用 Add 方法来添加成员,并使用 Remove 方法来删除成员。After a collection is created, members can be added by using the Add method and removed by using the Remove method. 若要返回集合中的特定成员,请使用 Item 方法;而若要循环访问整个集合,请使用 For Each...Next 语句。Specific members can be returned from the collection by using the Item method, while the entire collection can be iterated by using the For Each...Next statement.

示例Example

下面的示例先创建 Collection 对象 (MyClasses),再创建用户可向集合添加对象的对话框。This example creates a Collection object (MyClasses), and then creates a dialog box in which users can add objects to the collection.

若要查看具体运作方式,请选择“插入”**** 菜单中的“类模块”**** 命令,并在 Class1 模块级别声明公共变量 InstanceName(类型为 Public InstanceName),以保留每个实例的名称。To see how this works, choose the Class Module command from the Insert menu and declare a public variable called InstanceName at the module level of Class1 (type Public InstanceName) to hold the names of each instance. 保留默认名称 Class1。Leave the default name as Class1. Copy and paste the following code into the General section of another module, and then start it with the statement ClassNamer in another procedure.Copy and paste the following code into the General section of another module, and then start it with the statement ClassNamer in another procedure.

(下面的示例仅适用于支持类的主机应用程序。)(This example only works with host applications that support classes.)

Sub ClassNamer()
    Dim MyClasses As New Collection    ' Create a Collection object.
    Dim Num    ' Counter for individualizing keys.
    Dim Msg As String    ' Variable to hold prompt string.
    Dim TheName, MyObject, NameList    ' Variants to hold information.
    Do
        Dim Inst As New Class1    ' Create a new instance of Class1.
        Num = Num + 1    ' Increment Num, then get a name.
        Msg = "Please enter a name for this object." & vbNewLine _
         & "Press Cancel to see names in collection."
        TheName = InputBox(Msg, "Name the Collection Items")
        Inst.InstanceName = TheName    ' Put name in object instance.
        ' If user entered name, add it to the collection.
        If Inst.InstanceName <> "" Then
            ' Add the named object to the collection.
            MyClasses.Add item := Inst, key := CStr(Num)
        End If
        ' Clear the current reference in preparation for next one.
        Set Inst = Nothing
    Loop Until TheName = ""
    For Each MyObject In MyClasses    ' Create list of names.
        NameList = NameList & MyObject.InstanceName & vbNewLine
    Next MyObject
    ' Display the list of names in a message box.
    MsgBox NameList, , "Instance Names In MyClasses Collection"

    For Num = 1 To MyClasses.Count    ' Remove name from the collection.
        MyClasses.Remove 1    ' Since collections are reindexed automatically, remove the first member on each iteration.
    Next
End Sub

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.