0
我试图制作一个Excel宏,列出每个文件夹名称及其所有子文件夹下一列,类似于命令提示符树命令。我很期待看到的是这样的:Excel宏擦除单元格VBA
注意,文件夹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'。 – SJR