VBA/ Declare and initialize a array of structure

Babycat

Member
Local time
Today, 14:17
Joined
Mar 31, 2020
Messages
281
Dear all.

I have structure (type) in VBA and an array

Code:
Private Type Item_property
    Itemname As String
    ItemMaterial As String
End Type

Dim ItemArray() As Item_property
And I expect to init ItemArray like:
Code:
ItemArray = { ("Knife", "Steel"); ("Ruler", "Plastic"); ("Ring", "Gold")}
but It does not work, so I have to assign one by one:
Code:
ItemArray(0).Itemname = "Knife"
ItemArray(0).ItemMaterial = "Steel"
..so on...

Is there any way to declare and init array value shortly as i expected?
 
Yes, kinda, but not as a custom type. Either make it a class with a constructor which you can pass your two values to, or do away with the custom type and just make it an array of an array of strings. The array of array of strings would more similiarly follow the syntax you described you want. Less coding too.
 
Looks like you should be using a table instead of an array. UNLESS this is just a class assignment.
 

Users who are viewing this thread

Back
Top Bottom