我尝试使用vba将我的excel数据转换为树数据。在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代码树的数据转换这样的。
但是上面的代码并没有为我工作。
有人建议我吗?
感谢
我建议你开始编码,然后用回来当您遇到特定问题时提问。这不是一个代码工厂。顺便说一下,你试图制作的树与链接问题中的树不同,所以完全相同的方法不会起作用。 – OpiesDad
当您最初发布问题时,我解决了这个问题,但没有发布我的答案,因为您不会发布您的代码。现在,您将Christian Payne的答案发布到[在Excel中构建像数据表示的树?](http://stackoverflow.com/questions/1074004/build-a-tree-like-representation-of-data-in-excel)就好像它是你自己的! – 2016-12-21 16:20:02
对不使用数据透视表的解决方案感兴趣? – EEM