In our lessons, we will not create new collections. We will only use two categories: the Collection class and the built-in collection. To support collections, the Visual Basic language is equipped with a class named Collection.
This class can be used to create a collection. To do this, declare a variable of type Collection. Here is an example: Sub Exercise() Dim Employees As Collection End Sub After declaring the variable, to allocate memory for it, use the Set operator to assign a New instance to the variable. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection End Sub Instead of always creating a new collection unless you have to, VBA for Microsoft Excel comes equipped with many collections so that you almost may never need to create your own collection. The collections that are already built in the VBA language are referred to as built-in collections. The built-in collection classes are derived from the Visual Basic's Collection class. As a result, all of their primary functionality comes from the Collection class. This also means that everything we will mention for the Collection class applies to any built-in collection. To use a built-in collection, you can declare a variable for it. Here is an example: Sub Exercise() Dim CurrentSheets As Worksheets End Sub In reality, and as we will next in the next lessons, when Microsoft Excel starts, most (if not all) of the built-in collection classes are already available so that you do not have to declare their variable before using them.
The primary operation to perform on a collection consists of adding items to it. To support this, the Collection class is equipped with a method name Add. Its syntax is: Public Sub Add( _ ByVal Item As Object, _ Optional ByVal Key As String, _ Optional ByVal { Before | After } As Object = Nothing _ ) This method takes three arguments. Only the first is required. The Item argument specifies the object to be added to the collection. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" End Sub In the same way, you can add as many items as you want: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" End Sub Remember that if you are using one of the built-in collection classes, you do not have to declare a variable for it. You can just call the Add method on it to add an item to it. Here is an example: Sub Exercise() Worksheets.Add End Sub
|
|
||||||||||||||||||||||||
|
The items of a collection are organized in an arranged sequence where each item holds a specific index. the first item in the collection holds an index of 1. The second item holds an index of 2, and so on. To give you access to the items of a collection, the Collection class is equipped with a property named Item. There are two ways you can use this property. To formally use the Item property, type the name of the collection object, followed by the period operator, followed by Item and optional parentheses. After the Item property or inside its parentheses, type the index of the desired item. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item 2 End Sub Remember that you an also use parentheses: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item (2) End Sub Instead of using the Item property, you can apply the index directly to the collection object. Here are examples: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Item 2 Employees.Item (2) Employees 2 Employees (2) End Sub All these four techniques (notations) give you access to the item whose index you provided.
As opposed to adding a new item, you can delete one. To support this operation, the Collection class is equipped with a method name Remove. Its syntax is: Public Sub Remove(Index As Integer) This method takes one argument. When calling it, pass the index of the item you want to delete. Here is an example: Sub Exercise() Dim Employees As Collection Set Employees = New Collection Employees.Add "Patricia Katts" Employees.Add "James Wiley" Employees.Add "Gertrude Monay" Employees.Add "Helene Mukoko" Employees.Remove 2 End Sub This code deletes the second item in the collection.
When you start a new collection, obviously it is empty and its number of items is 0. To keep track of the number of items in a collection, the Collection class is equipped with a property named Count whose type is an integer. Remember that all built-in collection classes inherit their behavior from the Collection class. This means that the built-in collection classes are equipped with a property named Count to hold their number of items. We saw how you can add new items to a collection. Every time you add a new item to the collection, the Count property increases by 1. We also know how to remove an item from a collection. Whenever an existing item is deleted, the value of the Count property is decreased by 1. At anytime, to know the number of items that a collection is currently holding, get the value of its Count property. |
|
|||||||
|
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|