Array of Class Module Elements

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
103
I am looking to store instantiated class module objects into an array. This seems as if it should be simple but I have had no luck.

So if I had a class module called classPerson:
Code:
Public name As String
Public age As Integer

In the macro:
Code:
    Dim person As classPerson
    Set person = New classPerson
    person.name = personsname
    person.age = 10

    'store the person into an array
    Dim personArray(1 To 10) As classPerson
    Set personArray = New classPerson
    
    personArray(1) = personArray

This and variations of this without the Set either do not compile or crash. How is this type of thing handled in VBA?

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The last line should be:
Code:
Set personArray(1) = person
and remove this line:
Code:
Set personArray = New classPerson
 
Upvote 0
Thank you for the fast reply.

What is the secret in then retrieving the class from the array, as this doesn't work:

Code:
    ' now retrieve the first person from the array
    Dim personRetreivedFromArray As classPerson
    Set personRetreivedFromArray = New classPerson
    personRetreivedFromArray = personArray(1)

Thanks again
 
Upvote 0
Simply:
Code:
' now retrieve the first person from the array
    Dim personRetreivedFromArray As classPerson
    Set personRetreivedFromArray = personArray(1)
 
Upvote 0
Continuing with this code, how do you specify an array as a member variable of a class. In this case, the array will hold instantiated class modules:

classPerson:
Code:
Public name As String
Public age As Integer
Public carsOwned(1 To 10) As classCar
classCar:
Code:
Public vehicleMake As String
Public vehicleModel As String
Public vehicleYear As Integer
So each person will contain up to 10 classCar elements. This code doesn't compile, which is probably obvious to most of you.

Thanks
 
Upvote 0
Make the array private and use properties/methods to access it.
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,560
Members
449,237
Latest member
Chase S

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