如何将特定的层次结构树转换为标准的层次结构树

时间:2016-10-18 18:52:32

标签: excel vba excel-vba

我的用户以这种格式设计文件夹树:
Tree

我想将此文件夹hiearchy转换为“Windows”格式,如下所示:

  • Root \ Folder1 \ Folder11
  • Root \ Folder1 \ Folder12
  • 根\ Folder1中\ Folder13 ....

所以我在VBA做过:

Sub GenerateTree()
   Dim Level1 As String
   Dim Level2 As String
   Dim Level3 As String

   Dim i As Integer
   Dim num_line As Integer
   Dim Result() As String
   ReDim Result(i)

   'Init
   Level1 = ActiveSheet.Range("A2").Value    

   'For each value    For num_line = 2 To 11
        If Len(ActiveSheet.Range("B" & num_line).Value) > 0 Then 'If cell Bx contains something
            Level2 = ActiveSheet.Range("B" & num_line).Value 'Update Level2
        End If
            Level3 = ActiveSheet.Range("C" & num_line).Value

        'Result(num_line - 2) = Level1 & "/" & Level2 & "/" & Level3
        ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2 & "/" & Level3
    Next num_line

但我的结果并不好。 enter image description here

我的结果中有几个问题:

  • 对于Root/Folder2/,我/为此目的。我想Root/Folder2
  • 我想,我需要添加一个函数来了解num_line
  • 的最大值

我需要你的帮助

解决方案:

Sub GenerateTree()
   Dim Level1 As String
   Dim Level2 As String
   Dim Level3 As String

   Dim num_line_max As Integer
   Dim num_line As Integer
   Dim Result() As String
   ReDim Result(i)

   'Init
   Level1 = ActiveSheet.Range("A2").Value
   max_line_B = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
   max_line_C = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
   If max_line_B > max_line_C Then
      num_line_max = max_line_B
   Else
      num_line_max = max_line_C
   End If

   'For each value
   For num_line = 2 To num_line_max
        If Len(ActiveSheet.Range("B" & num_line).Value) > 0 Then 'If cell Bx contains something
            Level2 = ActiveSheet.Range("B" & num_line).Value 'Update Level2
        End If
            Level3 = ActiveSheet.Range("C" & num_line).Value

        If Level3 <> "" Then
            ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2 & "/" & Level3
        Else
            ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2
        End If
    Next num_line



End Sub

2 个答案:

答案 0 :(得分:0)

1)空值是因为IsNull将为任何单元格返回False,因为空单元格将被解释​​为""或0.使用IsEmpty或{ {1}}。 当然,你不应该使用cell.Value <> ""的支票,因为你有时希望它是空的。

2)要摆脱尾随Level3,您只需检查/是否为空

Level3

3)有很多方法可以找到最后一行。最简单的是

If Level3 <> "" Then
    ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2 & "/" & Level3
Else
    ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2
End If

在您的情况下,您必须检查B行和C行,然后选择较大的行。

答案 1 :(得分:0)

OP解决方案。

Sub GenerateTree()
   Dim Level1 As String
   Dim Level2 As String
   Dim Level3 As String

   Dim num_line_max As Integer
   Dim num_line As Integer
   Dim Result() As String
   ReDim Result(i)

   'Init
   Level1 = ActiveSheet.Range("A2").Value
   max_line_B = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
   max_line_C = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
   If max_line_B > max_line_C Then
      num_line_max = max_line_B
   Else
      num_line_max = max_line_C
   End If

   'For each value
   For num_line = 2 To num_line_max
        If Len(ActiveSheet.Range("B" & num_line).Value) > 0 Then 'If cell Bx contains something
            Level2 = ActiveSheet.Range("B" & num_line).Value 'Update Level2
        End If
            Level3 = ActiveSheet.Range("C" & num_line).Value

        If Level3 <> "" Then
            ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2 & "/" & Level3
        Else
            ActiveSheet.Range("F" & num_line) = Level1 & "/" & Level2
        End If
    Next num_line


End Sub