我想将此文件夹hiearchy转换为“Windows”格式,如下所示:
所以我在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
我的结果中有几个问题:
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
答案 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