Mic*_*los 7 treeview excel tree vba excel-vba
我有这样一堆原始数据:
Parent | Data
---------------
Root | AAA
AAA | BBB
AAA | CCC
AAA | DDD
BBB | EEE
BBB | FFF
CCC | GGG
DDD | HHH
Run Code Online (Sandbox Code Playgroud)
需要将其转换为时尚之树.这基本上需要在excel电子表格中结束.如何将以上数据转换为以下数据:
AAA | |
| BBB |
| | EEE
| | FFF
| CCC |
| | GGG
| DDD |
| | HHH
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法只使用VBA?
Chr*_*yne 12
我相信你可以整理一下,但这将适用于你提供的数据集.
在开始之前,您需要定义两个名称(插入/名称/定义)."数据"是数据集的范围,"目标"是您希望树到达的位置.
Sub MakeTree()
Dim r As Integer
' Iterate through the range, looking for the Root
For r = 1 To Range("Data").Rows.Count
If Range("Data").Cells(r, 1) = "Root" Then
DrawNode Range("Data").Cells(r, 2), 0, 0
End If
Next
End Sub
Sub DrawNode(ByRef header As String, ByRef row As Integer, ByRef depth As Integer)
'The DrawNode routine draws the current node, and all child nodes.
' First we draw the header text:
Cells(Range("Destination").row + row, Range("Destination").Column + depth) = header
Dim r As Integer
'Then loop through, looking for instances of that text
For r = 1 To Range("Data").Rows.Count
If Range("Data").Cells(r, 1) = header Then
'Bang! We've found one! Then call itself to see if there are any child nodes
row = row + 1
DrawNode Range("Data").Cells(r, 2), row, depth + 1
End If
Next
End Sub
Run Code Online (Sandbox Code Playgroud)