Sub MakeFamilyTree()
Dim arrName As Variant
Dim arrParent As Variant
Dim arrMatrix() As Variant
Dim arrTemp As Variant
Dim elm As Variant
Dim i As Long, j As Long
Dim ret As Variant
Dim node As node
Dim bExists As Boolean
'重置TreeView控件
UserForm1.TreeView1.Nodes.Clear
'从工作表中获取数据作为一个数组
With Sheets("Sheet1").Range(Sheets("Sheet1").[A2], Sheets("Sheet1").[A65536].End(xlUp))
arrName = .Value
arrParent = .Offset(, 1).Value
End With
'排序
ReDim arrMatrix(1 To UBound(arrName), 1 To 1)
For Each elm In arrParent
i = i + 1
ret = Application.Match(elm, arrName, 0)
If IsError(ret) Then
arrMatrix(i, 1) = arrName(i, 1)
Else
j = 3
ReDim Preserve arrMatrix(1 To UBound(arrMatrix), 1 To j)
arrMatrix(i, 1) = arrName(i, 1)
arrMatrix(i, 2) = elm
arrMatrix(i, 3) = arrParent(ret, 1)
Do
ret = Application.Match(arrParent(ret, 1), arrName, 0)
If IsError(ret) Then Exit Do
If arrParent(ret, 1) = "" Then Exit Do
j = j + 1
ReDim Preserve arrMatrix(1 To UBound(arrMatrix), 1 To j)
arrMatrix(i, j) = arrParent(ret, 1)
Loop
End If
Next
arrTemp = CustomTranspose(arrMatrix)
'添加数据到节点中
For i = 1 To UBound(arrTemp)
For j = 1 To UBound(arrTemp, 2)
If Not IsEmpty(arrTemp(i, j)) Then
With UserForm1.TreeView1
bExists = False
For Each elm In .Nodes
If elm = arrTemp(i, j) Then bExists = True
Next
If Not bExists Then
If j = 1 Then
Set node = .Nodes.Add(, , arrTemp(i, j), arrTemp(i, j), _
Image:=GetInfo(arrTemp(i, j), True))
Else
Set node = .Nodes.Add(arrTemp(i, j - 1), tvwChild, arrTemp(i, j), arrTemp(i, j), _
Image:=GetInfo(arrTemp(i, j), True))
End If
node.Expanded = True
End If
End With
End If
Next
Next
End Sub
Function CustomTranspose(ByVal buf As Variant) As Variant
'转换数组顺序从父节点到子节点
Dim arrTemp() As Variant
Dim i As Long, j As Long, k As Long
ReDim arrTemp(LBound(buf) To UBound(buf), LBound(buf, 2) To UBound(buf, 2))
For i = 1 To UBound(buf)
k = 0
For j = UBound(buf, 2) To 1 Step -1
If Not IsEmpty(buf(i, j)) Then
k = k + 1
arrTemp(i, k) = buf(i, j)
End If
Next
Next
CustomTranspose = arrTemp
End Function
Function GetInfo(sName, bAorD) As String
'返回合适的图像
Dim rFound As Range
Set rFound = Sheet1.Columns(1).Find(sName, lookat:=xlWhole)
If rFound Is Nothing Then
GetInfo = "none"
Else
GetInfo = IIf(bAorD, rFound.Offset(, 2).Value, rFound.Offset(, 3).Value)
End If
End Function
在用户窗体代码模块中,输入下面的代码:
Private Sub UserForm_Activate()
SettingImageList
MakeFamilyTree
End Sub
Private Sub SettingImageList()
Dim myImgList As New ImageList
Dim bt
With myImgList.ListImages
.Add Key:="Image1", Picture:=Sheet1.Image1.Picture
.Add Key:="Image2", Picture:=Sheet1.Image2.Picture
.Add Key:="Image3", Picture:=Sheet1.Image3.Picture
.Add Key:="Image4", Picture:=Sheet1.Image4.Picture
.Add Key:="Image5", Picture:=Sheet1.Image5.Picture
.Add Key:="Image6", Picture:=Sheet1.Image6.Picture
.Add Key:="Image7", Picture:=Sheet1.Image7.Picture
.Add Key:="Image8", Picture:=Sheet1.Image8.Picture
.Add Key:="Image9", Picture:=Sheet1.Image9.Picture
.Add Key:="none", Picture:=Sheet1.none.Picture
End With
With TreeView1
Set .ImageList = myImgList
.Indentation = 14
.LabelEdit = tvwManual
.HideSelection = False
End With
End Sub
声明:欢迎各大网站转载本站文章,还请保留一条能直接指向本站的超级链接,谢谢!
第1页 | 第2页 | 第3页 | 第4页 | 第5页 | 第6页 | 第7页时间:2007-9-26 21:11:51,点击:0
上一篇:在office Excel页眉页脚中插入图片【声明】:以上文章或资料除注明为Office吧自创或编辑整理外,均为各方收集或网友推荐所得。其中摘录的内容以共享、研究为目的,不存在任何商业考虑。如有任何异议,请与本站联系,联系邮箱:thinkou@126.com,本站确认后将立即撤下。谢谢您的支持与理解!
相关评论