【发布时间】:2016-12-02 22:25:59
【问题描述】:
我尝试使用 vba 将我的 excel 数据转换为树数据。
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
我的excel数据是这样的,
我尝试使用我的 vba 代码来转换这样的树数据。
但上面的代码对我不起作用。
有人推荐我吗?
谢谢
【问题讨论】:
-
我建议您开始编码,然后在遇到特定问题时再提出问题。这不是代码工厂。顺便说一句,您尝试制作的树与链接问题中的树不同,因此完全相同的方法无论如何都行不通。
-
当您最初发布问题时我已经解决了这个问题,但没有发布我的答案,因为您不会发布您的代码。现在您将 Christian Payne 的答案发布到 Build a tree like representation of data in Excel?,就好像它是您自己的一样!!!
-
对不使用数据透视表的解决方案感兴趣?