Home Accessing Object Properties in Array VBA
Reply: 1

Accessing Object Properties in Array VBA

Brian Phair
Brian Phair Published in 2017-12-07 14:12:44Z

I cannot figure out how to access object properties in an array using VBA. I have created an array like:

Dim objectArray(10) as Variant    
Dim counter as Integer            'used to move to next element in array

Next, I declared an object and store it in the array:

Dim object as Variant
objectArray(0) = object           'object stored in array[0]
counter = counter + 1             'increment counter

I want to pass the array to a function.

Call function(objectArray())

That function receives the array of objects like:

Public function(objectArray() as Variant)

So far, it seems to have worked when I have debugged it. My objectArray() seems to contain the object. When I store one object in the array, the debuger shows

objectArray(0)(1,1) .... 'this is in the Watch section of the debugger

I want to access the properties of that object in the first position of the array. That object will contain a name, several values, and a date. I've been trying to access the properties on that object like

Dim separateVar as Variant             'declare new var to hold "name"

separateVar = objectArray(0)(1,1).Value

However, when I run the macro, I get "Some Error Occured, 13, Type Mismatch". Am I accessing the object property values incorrectly?

Any help would be appreciated. Finding articles about accessing objects is easy, but finding ones about accessing their individual properties has been very difficult.

Kostas K.
Kostas K. Reply to 2017-12-07 14:43:47Z

Try this.

Sub T()
    Dim objInner1(1) As String
        objInner1(0) = "Hello"
        objInner1(1) = "World 1"

    Dim objInner2(1) As String
        objInner2(0) = "Hello"
        objInner2(1) = "World 2"

    Dim objInner3(1) As String
        objInner3(0) = "Hello"
        objInner3(1) = "World 3"

    Dim objOuter(2) As Variant
        objOuter(0) = objInner1
        objOuter(1) = objInner2
        objOuter(2) = objInner3

    PrintArray objOuter
End Sub

Sub PrintArray(objArray As Variant)
    Dim idx As Long
    For idx = LBound(objArray) To UBound(objArray)
        Debug.Print objArray(idx)(0) & " " & objArray(idx)(1)
    Next idx
End Sub

'Hello World 1
'Hello World 2
'Hello World 3
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.344452 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO