VBA and Macros 2013 - Creating a Collection in a Class Module

Status
Not open for further replies.

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 entitled "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:

Rich (BB code):
Private AllEmployees As New Collection


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

Rich (BB 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:

Rich (BB code):
Dim colEmployees as New clsEmployees


and later on the add method is used:

Rich (BB 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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Please familiarise yourself with the forum rules: MrExcel Message Board FAQ
•Avoid multiple questions of a similar nature. Duplicate posts by the same user will be locked and/or deleted when found. If the answer you receive is inadequate for some reason, post a reply stating why with more information (if/when needed) in the same thread. Do not start a new topic

Thread closed.
 
Status
Not open for further replies.

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

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