|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
不用嵌套那么麻烦,一个字典就搞定了
- Private Sub UserForm_Initialize()
- Dim arr, d As Object
- Dim i As Long, j As Long, str As String, strKey As String
- arr = Range("A2:D" & Cells(Rows.Count, 1).End(xlUp).Row).Value
- TreeView1.Nodes.Add , , "乡镇", "乡镇"
- Set d = CreateObject("Scripting.Dictionary")
- For i = LBound(arr) To UBound(arr)
- str = "乡镇"
- For j = LBound(arr, 2) To UBound(arr, 2)
- strKey = str
- str = str & arr(i, j)
- If Not d.Exists(str) Then
- d.Add str, arr(i, j)
- TreeView1.Nodes.Add strKey, tvwChild, str, arr(i, j)
- End If
- Next j
- Next i
- Erase arr
- Set d = Nothing
- End Sub
复制代码 |
|