XL97: How to Programmatically Create a CollectionLast reviewed: February 27, 1998Article ID: Q161215 |
The information in this article applies to:
SUMMARYNew programming functionality in Microsoft Excel 97 allows you to create a collection. A collection is a predefined object that stores groups of related objects. A collection makes it easier to work with the object group. For example, you can use a For Each looping structure to loop through the collection. Each time the macro executes the loop it references a different object in the collection until all objects in the collection are referenced once. This article includes a sample Visual Basic for Applications macro that creates and references a collection.
MORE INFORMATIONMicrosoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400. In general, declare an object as a new collection to create the collection. After you create the Collection object, add items to the collection using the Add method or remove items using the Remove method.
For Each x In employees employees.Remove 1 'Remove each employee from thecollection. Next MsgBox employees.Count 'Display a count of zero because 'all employees were removed from the 'collection. End Sub typing names. Message boxes that display each of the names you typed appear. Then, a message box that displays a count of the names you typed appears. Another message box with a count of zero appears because the last For Each loop removes each employee from the collection.
REFERENCESFor more information about the Add Method, click the Index tab in Visual Basic for Applications Help, type the following text
add methodand then double-click the selected text to go to the "Add Method (Visual Basic for Applications)" topic. For more information about Collections, click the Index tab in Visual Basic for Applications Help, type the following text
collections, describedand then double-click the selected text to go to the "Collection Object" topic.
|
Additional reference words: 97 for Windows XL97
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |