Why does this work:
Sub f1(a() as Variant)
Debug.print "Ok"
End Sub
Sub main()
Dim a(2) as Variant
Dim a2(2) as Variant
a(0) = a2
f1 a2
End Sub
But this doesn't:
Sub f1(a() as Variant)
Debug.print "Ok"
End Sub
Sub main()
Dim a(2) as Variant
Dim a2(2) as Variant
a(0) = a2
f1 a(0)
End Sub
The objective is to manipulate arrays or arrays in VBA, recursively, and to be able to call functions on subpart of the tree. But it looks like when I call arr(0), VBA thinks it is a simple Variant, because arr has the type of an array of Variant (not an array of arrays of variants). Thus, it doesn't see that arr(0) it is an array by itself. And it panics.
I don't know how to make VBA understand that it's ok, he can execute the code, there is no type issue here.
I also didn't find a way to declare a type as an "array of array of variant" to make things clear from the start.
Note: I finally found a way to bypass the problem by : creating a variable of the right type, putting its value to the element of the table and use it. Example here, by adding the value "t" :
Sub f1(a() as Variant)
Debug.print a(0)
End Sub
Sub main()
Dim a(2) as Variant
Dim a2(2) as Variant
a2(0) = "It works!"
a(0) = a2
Dim t() as Variant
t = a(0)
f1 t
End Sub
But it looks wrong : I should be able to declare the types clearly enough from the start and I should not have to re-cast my types when I want to use them.
So, any idea on how to do it the best way ?
See description. I want to understand how to manipulate arrays of arrays of variant in VBA, recursively, the proper way without type issues.