I am trying to make an excel macro that lists each folder name and all of its subfolders on the next column, similar to the command prompt tree command. What I'm expecting to see is something like this:
Note, folders 2 4 and 5 have no folders in them, only files and I don't want this to display files. I've turned screen updating back on so I can see what it does and it does list the folders with no subfolders in the correct place, but then immediately overwrites them so I'm left with this:
Here's the code
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
if there are no sub-folders. – SJR