【发布时间】:2017-07-20 14:24:59
【问题描述】:
我正在尝试制作一个 excel 宏,该宏在下一列中列出每个文件夹名称及其所有子文件夹,类似于命令提示符树命令。我期望看到的是这样的:
注意,文件夹 2 4 和 5 中没有文件夹,只有文件,我不希望它显示文件。我已经重新打开屏幕更新,所以我可以看到它做了什么,它确实在正确的位置列出了没有子文件夹的文件夹,但随后立即覆盖了它们,所以我只剩下这个:
这是代码
Sub Example2()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer, j As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("Y:filepath")
i = 1
j = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
'print folder name
Cells(i + 1, 1) = objSubFolder.Name
'print folder path
'Cells(i + 1, 2) = objSubFolder.Path
For Each objsubsubfolder In objSubFolder.subfolders
Cells(i + 1, j + 1) = objsubsubfolder.Name
i = i + 1
Next objsubsubfolder
Next objSubFolder
End Sub
【问题讨论】:
-
如果没有子文件夹,您的循环不会增加
i。