例子
[01]一级目录
[02]二级目录
[3]三级目录
内容1
内容2
..............................................
...............................................
如何使用vba来获取以上的目录呢?
Sub main()
Dim line As Integer
Dim row As Integer
dic_index = 0
row = 1
For line = 0 To 16
line = line + 1
If Worksheets("sheet1").Cells(line, row) = "" And Worksheets("sheet1").Cells(line, row + 1) = "" Then
row = getback(line, row, dic_index)
line = line - 1
End If
If Worksheets("sheet1").Cells(line, row) = "" And Worksheets("sheet1").Cells(line, row + 1) <> "" Then
row = row + 1
line = line - 1
End If
If Worksheets("sheet1").Cells(line, row) <> "" Then
dic_arr(dic_index) = Worksheets("sheet1").Cells(line, row)
dic_index = dic_index + 1
If Worksheets("sheet1").Cells(line + 1, row + 1) <> "" Then
If Worksheets("sheet1").Cells(line + 2, row + 2) <> "" Then
'diction requary
End If
End If
Else
'cope data
End If
Next
End Sub
Function getback(line As Integer, row As Integer, dic_index As String)
Do While Worksheets("sheet1").Cells(line, row) = ""
row = row - 1
dic_index = dic_index - 1
Loop
row = getback
End Function
以上代码参考
VBA获取目录方法探讨
本文介绍了如何使用VBA(Visual Basic for Applications)来获取Excel文档中的目录结构,包括一级、二级和三级目录。通过示例代码,详细展示了实现这一功能的方法。
935

被折叠的 条评论
为什么被折叠?



