7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command1_Click()
Dim ParentCollection As New Collection '父集合
Dim ChildCollection As New Collection '子集合
Dim i As Integer, j As Integer
For i = 1 To 30
ChildCollection.Add "项" & CStr(i)
Next
ParentCollection.Add ChildCollection '父集合添加子集合
Dim tempCollection As New Collection '临时集合,用于遍历得到父集合中的子集合
For i = 1 To ParentCollection.Count
Set tempCollection = ParentCollection.Item(i)
For j = 1 To tempCollection.Count
Debug.Print tempCollection.Item(j)
Next
Next
End Sub
Option Explicit
Private Sub Command1_Click()
Dim ParentCollection As New Collection '父集合
Dim ChildCollection As New Collection '子集合
Dim ChildCollection2 As New Collection '孙集合
Dim i As Integer, j As Integer
For i = 1 To 30
ChildCollection2.Add "孙子" & i
Next
For i = 1 To 15
ChildCollection.Add "儿子" & i
Next
ParentCollection.Add ChildCollection '父集合添加子集合
ChildCollection.Add ChildCollection2 '子集合添加孙子集合
Call OutputCol(ParentCollection)
End Sub
Private Sub OutputCol(ParentC As Collection, Optional CLevel As Long = 0)
Dim i As Long
If CLevel = 0 Then Debug.Print "根结点"
For i = 1 To ParentC.Count
If TypeOf ParentC.Item(i) Is Collection Then
Debug.Print String(CLevel + 1, "-") & ">"
Call OutputCol(ParentC.Item(i), CLevel + 1)
Else
Debug.Print String(CLevel, " ") & ParentC.Item(i)
End If
Next
End Sub