Dim InputArray As Variant
Dim InputArraySorted As Variant
Dim Flip() As Integer
Sub MakeTreeView()
Dim SortOrderBy As Integer
GetInputArray
SortInputArray
AddNodes
PopulateCombos
frmTreeView.Show
End Sub
Sub AddNodes()
Dim NewNode As Node
Dim OldText() As String
Dim NodeParent() As String
ReDim Flip(1 To UBound(InputArray, 1), 1 To UBound(InputArray, 2))
ReDim NodeParent(1 To UBound(InputArray, 1), 1 To UBound(InputArray, 2))
frmTreeView.TreeView1.Nodes.Clear
For j = 1 To UBound(InputArray, 2)
Flip(1, j) = 1
Next
For i = 2 To UBound(InputArray, 1)
For j = 1 To UBound(InputArray, 2)
If InputArray(i, j) = InputArray(i - 1, j) Then
If j > 1 Then
If Flip(i, j - 1) = 1 Then
Flip(i, j) = 1
Else
Flip(i, j) = 0
End If
Else
Flip(i, j) = 0
End If
Else
Flip(i, j) = 1
End If
Next
Next
For j = 1 To UBound(InputArray, 2)
NodeCount = 1
For i = 1 To UBound(InputArray, 1)
NodeKey = "NK" & j & NodeCount
If j = 1 Then
If Flip(i, j) = 1 Then
Set NewNode = frmTreeView.TreeView1.Nodes.Add(, , NodeKey, InputArray(i, j))
NodeCount = NodeCount + 1
NodeParent(i, j + 1) = NodeKey
Else
NodeParent(i, j + 1) = NodeParent(i - 1, j + 1)
End If
Else
Debug.Print i, j, NodeKey, InputArray(i, j)
If Flip(i, j) = 1 Then
Set NewNode = frmTreeView.TreeView1.Nodes.Add(NodeParent(i, j), tvwChild, NodeKey, InputArray(i, j))
NodeCount = NodeCount + 1
If j < UBound(InputArray, 2) Then NodeParent(i, j + 1) = NodeKey
Else
If j < UBound(InputArray, 2) Then NodeParent(i, j + 1) = NodeParent(i - 1, j + 1)
End If
End If
Next
Next
frmTreeView.lblSummary.Caption = "This view has " & frmTreeView.TreeView1.Nodes.Count & " Nodes"
End Sub
Sub GetInputArray()
ActiveSheet.Activate
ActiveCell.Select
Selection.CurrentRegion.Select
InputArray = Selection.Value
End Sub
Sub SortInputArray()
Dim SortOrderBy As Integer
SortOrderBy = 1
InputArraySorted = MultiColumnSort(InputArray, SortOrderBy)
InputArray = InputArraySorted
End Sub
Sub MakeXLS()
Sheets.Add
Cells(3, 1).Select
For i = 1 To UBound(InputArray, 1)
'j contains the hierarchy level --> level 4 is the lowest hierarchy
j = 4 '1 To UBound(InputArray, 2)
If Flip(i, j) = 1 Then
Cells(i + 2, j).Value = InputArray(i, j)
End If
Next
'Next
'ActiveSheet.Columns("A:Z").AutoFit
'ActiveSheet.Buttons.Add(0.75, 1.5, 21, 15).Select
'Selection.OnAction = "ExpandTree"
'Selection.Characters.Text = "+"
'ActiveSheet.Buttons.Add(22.5, 1.5, 21, 15).Select
'Selection.OnAction = "ContractTree"
'Selection.Characters.Text = "-"
'Cells(3, 1).Select
End Sub