问题
I have two collections, the second one is inside the first one. But when I try to access the element of the second collection I get a Type mismatch
error.
Sub testColls()
Dim coll1 As Collection
Dim coll2 As Collection
Set coll1 = New Collection
Set coll2 = New Collection
coll2.Add ("dog")
coll1.Add ("cat")
coll1.Add coll2
Dim temp As String
temp = coll1(1)(1)
MsgBox (temp)
End Sub
Why is this error? coll1(1)
gets the second collection, coll
(1)(1)` should give first element of the second collection.
回答1:
Instead of
temp = coll1(1)(1)
use
temp = coll1(2)(1)
Using coll1(2)(1)
will give dog
and coll1(1)
will give cat
.
To make it more readable you can use coll1.Item(2).Item(1)
for coll1(2)(1)
and likewise coll1.Item(1)
for coll1(1)
来源:https://stackoverflow.com/questions/46191127/nested-collections-access-elements-type-mismatch