我无法弄清楚如何使用VBA访问数组中的对象属性。我创建了一个类似的数组:
Dim objectArray(10) as Variant
Dim counter as Integer 'used to move to next element in array
接下来,我声明了一个对象并将其存储在数组中:
Dim object as Variant
objectArray(0) = object 'object stored in array[0]
counter = counter + 1 'increment counter
我想将数组传递给函数。
Call function(objectArray())
该函数接收对象数组,如:
Public function(objectArray() as Variant)
到目前为止,它似乎在我调试它时起作用了。我的objectArray()似乎包含该对象。当我在数组中存储一个对象时,debuger显示
objectArray(0)(1,1) .... 'this is in the Watch section of the debugger
我想在数组的第一个位置访问该对象的属性。该对象将包含名称,多个值和日期。我一直试图访问该对象上的属性,如
Dim separateVar as Variant 'declare new var to hold "name"
separateVar = objectArray(0)(1,1).Value
然而,当我运行宏时,我得到"一些错误发生,13,类型不匹配"。我是否错误地访问了对象属性值?
任何帮助将不胜感激。查找有关访问对象的文章很容易,但找到有关访问其各自属性的文章非常困难。
答案 0 :(得分:0)
试试这个。
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