|
楼主 |
发表于 2013-11-30 04:07
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 zhaogang1960 于 2013-12-6 01:47 编辑
二、用TreeView控件建立目录树
字典嵌套代码由chxw68坛友所写,原代码如下:
Private Sub UserForm_Initialize() '使用此段程序,必须在VBE中先加载"Micerosoft TreeView Conntrol,version6.0"控件。
Dim d As New Dictionary '建立字典
Dim i, j, r, c As Integer
Dim ws As Worksheet
Dim nodex As Node
With TreeView1 '设置TreeView控件属性
.Nodes.Clear
.Style = 6
.LineStyle = 1
End With
Set ws = Worksheets("sheet1")
r = Cells(Rows.Count, 1).End(xlUp).Row
arr = Range("a2:d" & r)
n = 0
For i = 1 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = CreateObject("scripting.dictionary") '一级字典嵌套
End If
If Not d(arr(i, 1)).Exists(arr(i, 2)) Then
Set d(arr(i, 1))(arr(i, 2)) = CreateObject("scripting.dictionary") '二级字典嵌套
End If
If Not d(arr(i, 1))(arr(i, 2)).Exists(arr(i, 3)) Then
Set d(arr(i, 1))(arr(i, 2))(arr(i, 3)) = CreateObject("scripting.dictionary") '三级字典嵌套
End If
If Not d(arr(i, 1))(arr(i, 2))(arr(i, 3)).Exists((arr(i, 4))) Then
Set d(arr(i, 1))(arr(i, 2))(arr(i, 3))(arr(i, 4)) = CreateObject("scripting.dictionary") '四级字典嵌套
End If
Next
TreeView1.Nodes.Clear
Set nodex = TreeView1.Nodes.Add(, , "乡镇", "乡镇") '添加根节点
For Each aa In d.Keys
i = i + 1
Set nodex = TreeView1.Nodes.Add("乡镇", tvwChild, aa & i, aa) '添加二级节点
For Each bb In d(aa).Keys
j = j + 1
Set nodex = TreeView1.Nodes.Add(aa & i, tvwChild, bb & j, bb) '添加三级节点
For Each cc In d(aa)(bb).Keys
k = k + 1
Set nodex = TreeView1.Nodes.Add(bb & j, tvwChild, cc & k, cc) '添加四级节点
For Each dd In d(aa)(bb)(cc).Keys
l = l + 1
Set nodex = TreeView1.Nodes.Add(cc & k, tvwChild, dd & l, dd) '添加五级节点
Next
Next
Next
Next
End Sub
用一个字典实现代码如下:
Private Sub UserForm_Initialize()
'本例仅说明可以用一个字典实现多级联动,没有比较速度,因为时间主要浪费在向TreeView写数据
Dim d As New Dictionary '建立字典
Dim nodex As Node
Dim arr, aa, bb, cc, dd, i&, j&
With TreeView1 '设置TreeView控件属性
.Nodes.Clear
.Style = 6
.LineStyle = 1
arr = Range("a2:d" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = 1 To UBound(arr)
For j = 1 To 3 '从第一级到倒数第二级,用循环表示
If j = 1 Then s = arr(i, j) Else s = s & vbTab & arr(i, j)
If Not d.Exists(s) Then
d(s) = arr(i, j + 1)
Else
If InStr("," & d(s) & ",", "," & arr(i, j + 1) & ",") = 0 Then d(s) = d(s) & "," & arr(i, j + 1)
End If
Next
'以上用For循环实现下面注释部分设置字典
' s = arr(i, 1)
' If Not d.Exists(s) Then
' d(s) = arr(i, 2)
' Else
' If InStr("," & d(s) & ",", "," & arr(i, 2) & ",") = 0 Then d(s) = d(s) & "," & arr(i, 2)
' End If
' s = s & vbTab & arr(i, 2)
' If Not d.Exists(s) Then
' d(s) = arr(i, 3)
' Else
' If InStr("," & d(s) & ",", "," & arr(i, 3) & ",") = 0 Then d(s) = d(s) & "," & arr(i, 3)
' End If
' s = s & vbTab & arr(i, 3)
' If Not d.Exists(s) Then
' d(s) = arr(i, 4)
' Else
' If InStr("," & d(s) & ",", "," & arr(i, 4) & ",") = 0 Then d(s) = d(s) & "," & arr(i, 4)
' End If
Next
.Nodes.Clear
Set nodex = .Nodes.Add(, , "乡镇", "乡镇") '添加根节点
For Each aa In Filter(d.Keys, vbTab, False) '字典键值不含vbTab数组
i = i + 1
Set nodex = .Nodes.Add("乡镇", tvwChild, aa & i, aa) '添加二级节点
For Each bb In Split(d(aa), ",")
j = j + 1
Set nodex = .Nodes.Add(aa & i, tvwChild, bb & j, bb) '添加三级节点
For Each cc In Split(d(aa & vbTab & bb), ",")
k = k + 1
Set nodex = .Nodes.Add(bb & j, tvwChild, cc & k, cc) '添加四级节点
For Each dd In Split(d(aa & vbTab & bb & vbTab & cc), ",")
l = l + 1
Set nodex = .Nodes.Add(cc & k, tvwChild, dd & l, dd) '添加五级节点
Next
Next
Next
Next
End With
End Sub
附件二——建立目录树(字典四级嵌套和用一个字典分别实现).rar
(23.35 KB, 下载次数: 2540)
建立目录树(字典四级嵌套和用一个字典分别实现从第一级到倒数第二级,用循环表示).rar
(18.86 KB, 下载次数: 2194)
|
评分
-
4
查看全部评分
-
|