用vba解析递归树

时间:2018-10-01 16:25:52

标签: excel vba excel-vba

给出以下数据电子表格:https://ethercalc.org/q7n9zwbzym5y

我有以下代码将对此进行解析,并将从工作表中的父子关系派生出一棵树。请注意,每个列出现两次的事实是因为列的第一个实例是针对另一种数据类型的,所以我只关心填充的列。这是上面工作表中所需的输出: enter image description here

代码:

Sub performanceSheet(someParams)
' Write to "Performance" sheet
    Dim w1 As Worksheet, w2 As Worksheet, wsSearch As Worksheet, wsData As Worksheet
    Dim num_rows
    Dim parent As Range, parentName As String
    Dim parentRange As Range, childrenRange As Range
    Dim childCount As Long
    Dim p As Variant
    Dim f1 As Range, f2 As Range
    currRow = 8


    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Set w1 = wbk.Sheets("PositionsDB")
    Set w2 = wbk.Sheets("Performance")

    num_rows = w1.Cells(Rows.Count, 1).End(xlUp).row
    'If there's no parentName column, we can't continue.
    If w1.Rows(1).Find("portfolioName") Is Nothing Then Exit Sub

    'find first instance
    Set f1 = w1.Rows(1).Find("portfolioName", lookat:=xlWhole)
    If Not f1 Is Nothing Then
        'find second instance
        Set f2 = f1.Offset(0, 1).Resize(1, w1.Columns.Count - f1.Column).Find("portfolioName", lookat:=xlWhole)
        If Not f2 Is Nothing Then
            'set range based on f2
            Set parentRange = w1.Range(f2.Offset(1, 0), _
                                       w1.Cells(Rows.Count, f2.Column).End(xlUp))

        End If
    End If
    'If there's no Root level, how do we know where to start?
    If parentRange.Find("Main") Is Nothing Then Exit Sub

    For Each parent In parentRange
        If Not dict.Exists(parent.Value) Then
            childCount = Application.WorksheetFunction.CountIf(parentRange, parent.Value)
            Set childrenRange = parent.Offset(, 2).Resize(childCount, 1)
            dict.Add parent.Value, Application.Transpose(Application.Transpose(childrenRange.Value))
        End If
    Next
    ' Recursive method to traverse our dictionary, beginning at Root element.
    Call PerformanceProcessItem("", "Main", dict, w2, 9)

    wbk.Sheets("Performance").Columns("A:F").AutoFit

End Sub


Private Sub PerformanceProcessItem(parentName As String, name As String, dict As Object, ws As Worksheet, row_num As Long, Optional indent As Long = 0)
    Dim output As String, v
    Dim w2 As Worksheet


    'Debug.Print WorksheetFunction.Rept(" ", indent) & name
    'Debug.Print parentName & name

    'write to sheet
    ws.Cells(row_num, 3).Value = name

    row_num = row_num + 1
    If Not dict.Exists(name) Then
        'we're at a terminal element, a child with no children.
        Exit Sub
    Else
            For Each v In dict(name)
                ' ## RECURSION ##
                Call PerformanceProcessItem(name, CStr(v), dict, ws, row_num, indent + 2)
            Next
    End If

End Sub

但是,当创建此树时,它陷入了印度的无限循环中,在该树中,将“现金”识别为印度的终端元素之后,与其退出该子树,它会创建另一个印度并继续直到溢出。我的代码中是否存在逻辑错误?数小时的调试对我来说没有用,如果我在逻辑上有缺陷,请多多投入。

1 个答案:

答案 0 :(得分:1)

我假设“主要”和“现金”将始终存在。如果没有,那么我们将不得不微调代码。我已经注释了代码,因此您可能对理解它没有任何问题。但是,如果您这样做,只需问一下。我很快编写了这段代码,因此我确定可以对其进行优化:)

Option Explicit

Dim sB As String
Dim tmpAr As Variant

Sub Sample()
    Dim col As New Collection
    Dim s As String
    Dim ws As Worksheet
    Dim lRow As Long, i As Long, j As Long
    Dim itm As Variant, vTemp As Variant

    Set ws = Sheet1 '<~~ Change this to the relevant sheet

    With ws
        '~~> Get Last Row of Col AA
        lRow = .Range("AA" & .Rows.Count).End(xlUp).Row
        '~~> Store Range AA:AC in an array
        tmpAr = .Range("AA2:AC" & lRow).Value
    End With

    '~~> Create a unique collection of portfolioName
    For i = LBound(tmpAr) To UBound(tmpAr)
        If tmpAr(i, 1) = "Main" Then
            On Error Resume Next
            col.Add tmpAr(i, 3), CStr(tmpAr(i, 3))
            On Error GoTo 0
        End If
    Next i

    '~~> Sort the collection
    For i = 1 To col.Count - 1
         For j = i + 1 To col.Count
             If col(i) > col(j) Then
                vTemp = col(j)
                col.Remove j
                col.Add vTemp, vTemp, i
             End If
         Next j
    Next i

    s = "Main"

    For Each itm In col
        sB = vbTab & itm
        s = s & vbNewLine & sB
        sB = ""
        GetParentChild itm, 2
        If Trim(sB) <> "" Then _
        s = s & vbNewLine & sB
    Next itm
    s = s & vbNewLine & vbTab & "Cash"
    Debug.Print s
End Sub

Private Sub GetParentChild(strg As Variant, n As Integer)
    Dim sTabs As String
    Dim j As Long, k As Long

    For k = 1 To n
        sTabs = sTabs & vbTab
    Next k

    For j = LBound(tmpAr) To UBound(tmpAr)
        If Trim(tmpAr(j, 1)) = Trim(strg) And Trim(tmpAr(j, 1)) <> "Cash" Then
            sB = sB & sTabs & tmpAr(j, 3) & vbNewLine

            GetParentChild tmpAr(j, 3), n + 1
        End If
    Next j
End Sub

这是我在您提供的数据上运行它时得到的。

enter image description here