Excel VBA Collection Object
In VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task., apart from the existing collection of items under one group, we can create our collection groups. In our many articles, we have spoken about object variables, and in this tutorial, we will take through the VBA collection object in detail.
If you have gone through our previous article “VBA Arrays,” this will be much easier for you to understand. Arrays are used to group variables under one roof; similarly, Collection is also used to store a group of variables.
Collections are used to store objects. They are far more flexible than the VBA arrays, whereas arrays have fixed size limits, but readers don’t have any fixed size limit at any given point in time and even don’t require manual resizing.
VBA Collection is very similar to the “VBA Dictionary,” but the dictionary requires external object reference to be set it up under the object reference window. With VBA Dictionary, we need to set the reference type as “Microsoft Scripting Runtime,” but Collection doesn’t require any extra fittings.
How to Create Collection Object in VBA?
To get started with collection first, we need to declare the variable as “Collection.”
Sub Collection_Example() Dim Col As Collection End Sub
Since the collection is an object variable, we need to set the object reference by creating a new instance.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection End Sub
Now with the variable, we can access all the methods of collection variable “Col.”
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col. End Sub
Before making use of these methods, we need to declare a variable as a string.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Dim ColResult As String End Sub
Now use the variable “Col” to choose the “Add” method.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add End Sub
Under the Add method, we have specific parameters. Let’s assume we are storing mobile brand names with their average selling price in the market.
Under Item, the argument enters the price of the mobile.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add Item:=15000, End Sub
Next, under Key argument, enter the mobile brand name.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add Item:=15000, Key:="Redmi" End Sub
For the variable “ColResult,” we will store the result of the “Col” object variable.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add Item:=15000, Key:="Redmi" ColResult = Col( End Sub
When you open the parenthesis of the variable “Col,” we can see the argument as Index. For this argument, we need to supply the critical argument value from the Collection add method, i.e., the name of the mobile brand.
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add Item:=15000, Key:="Redmi" ColResult = Col("Redmi") End Sub
Now let show the result in the message box in VBAMessage Box In VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided..
Sub Collection_Example() Dim Col As Collection Set Col = New Collection Col.Add Item:=15000, Key:="Redmi" ColResult = Col("Redmi") MsgBox ColResult End Sub
Ok, we are done when we run the code. We should see the price of the mobile brand, “Redmi.”
Better Understanding of Key & Item Parameters
I am sure it is not easy to understand the parameters of the Collection object. Let me explain to you a simple example.
Imagine you have a menu of fruits with their name and price of the fruits. Assume you are searching the “Apple” fruit price by the name of the fruit.
To search the price of the fruit, we need to mention the name of the fruit, i.e., in collection VBA language Name of the Fruit is Key, and the price of the fruit is “Item.”
It is like applying the VLOOKUP or HLOOKUP function, based on the lookup value, and we will fetch the required data from the database. Here lookup value is Key, and the result is Item.
Imagine you are a store manager in one of the retail stores, and you are responsible for managing the customer queries. One such customer query is an inquiry about the product price.
It would help if you allowed the customer to search the price of the product with complete information. Similarly, you need to show the message in case of no data found. Below is the example code which will present the input box in front of the user. They require to enter the name of the product they are looking for. If the product is there in the collection, it will show the price of the mentioned product, or else it will show the message as “The Product you are searching for doesn’t exist.”
Sub Collection_Example2() Dim ItemsCol As Collection Dim ColResult As String Set ItemsCol = New Collection ItemsCol.Add Key:="Apple", Item:=150 ItemsCol.Add Key:="Orange", Item:=75 ItemsCol.Add Key:="Water Melon", Item:=45 ItemsCol.Add Key:="Mush Millan", Item:=85 ItemsCol.Add Key:="Mango", Item:=65 ColResult = Application.InputBox(Prompt:="Please Enter the Fruit Name") If ItemsCol(ColResult) <> "" Then MsgBox "The Price of the Fruit " & ColResult & " is : " & ItemsCol(ColResult) Else MsgBox "Price of the Fruit You are Looking for Doesn't Exists in the Collection" End If End Sub
This has been a guide to VBA Collection. Here we learn how to create a VBA collection object and Advanced examples, and a downloadable excel template. Below are some useful excel articles related to VBA –