Array of Class Module Elements

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
105
Office Version
  1. 365
Platform
  1. Windows
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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