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