VBA Creating a Collection in a Class Module

drj30026abanba

Board Regular
Joined
Oct 22, 2015
Messages
83
Hi this is my first time posting on this forum so I hope I am putting this in the right place. I am currently reading MrExcel's "VBA and Macros: Microsoft Excel 2013" and I am having trouble understanding a section in chapter 9 called "Creating a Collection in a Class Module".

Earlier in the chapter a custom object called clsEmployee was created. A class module is then inserted and renamed clsEmployees. Then a private collection is declared in the class module

Code:
Private AllEmployees As New Collection

After that, a few methods are created. Here is one of them

Code:
Public Sub Add(recEmployee As clsEmployee)

AllEmployees.Add recEmployee, recEmployee.EmpID

End Sub

Later in this section, a procedure in a standard module is written to use this collection. It is instantiated like this

Code:
Dim colEmployees as New clsEmployees

and later on the add method is used:

Code:
colEmployees.Add recemployee

I don't understand why clsEmployees is the name being used to instantiate colEmployees. That is the name of the class module just created. Is the name of the class module always used to instantiate? Why isn't AllEmployees declared Public and used since it is how all the methods are defined?

I don't get why AllEmployees is Private. Also, I don't understand how clsEmployees is related to AllEmployees. AllEmployees looks like a dummy variable but I don't understand how VBA knows that.

I guess my main question is this: When creating a collection with a class module, how is the name of the class module related to the private declaration of a collection?
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
It's not. The collection name can be anything you like as can the class name.
 
Upvote 0
Welcome to the Forum.

It's probably worth noting first that you can use a collection to store multiple instances of a class. So you could, for example, do something like this:

Code:
Dim colEmployees As Collection
Dim Employee As clsEmployee

Set colEmployees = New Collection
Set Employee = New clsEmployee

'Set properties for Employee, eg .Name, .EmpID perhaps
colEmployees.Add Employee, Key:=Employee.Empid

Instead, here another Class called clsEmployees has been created. AllEmployees is a collection within each instance of clsEmployees. The approach should make for more robust coding, e.g. it better controls what can be put into the employees collection.
 
Upvote 0
Thanks for the replies!

StephenCrump, the code you wrote is actually exactly what the book says to do to create a collection in a standard module. That makes sense to me but I don't understand what the book is doing when they created the collection in a class module. Is the AllEmployees collection a default property of the class clsEmployees because it's the only thing declared? Is that why even though the methods are defined with AllEmployee they are able to be used by clsEmployees objects without reference to AllEmployee?
 
Last edited:
Upvote 0
The AllEmployees collection is private and cannot be directly accessed from outside the clsEmployees class.

The clsEmployees class has its own Public Add method declared which adds the clsEmployee objects to the AllEmployees collection, and it is that Add method that is called from outside the class.
 
Upvote 0
Thanks for the explanation RoryA! I didn't understand how that public add method worked until you explained it. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top