Here is the main structure that you probably want. Depending on if you want to process the first folder (Option 1) or just the Sub-Folders (Option 2); choose the respective option for placing your code (replacing Debug.Print Path & Folder
)
Main Function:
Sub MainListFolders()
ListFolders ("C:\Temp\")
End Sub
Recursive Function:
Sub ListFolders(Path As String)
Dim Folder As String
Dim FolderList() As String
Dim i As Long, Count As Long
Folder = Dir(Path, vbDirectory)
' Option 1: Can process folder here
'Debug.Print Path & sFolder
Do While Folder <> vbNullString
' Check that it is a Folder
If CBool(GetAttr(Path & Folder) And vbDirectory) Then
' We don't want to include the Current (".") or Previous ("..") folders, so..
If Replace(Folder, ".", vbNullString) <> vbNullString Then
' Option 2: Can process folder here
Debug.Print Path & Folder
' Store the list of Sub-Folders to recursively check at the end
' If you try to do a recursive call here, when it jumps back, it wont be able to process the next Dir()
' because the Dir() folder would have changed in the recurive call.
ReDim Preserve FolderList(Count)
FolderList(Count) = Folder
Count = Count + 1
End If
End If
Folder = Dir()
Loop
' Do the recursive calls here
For i = 0 To Count - 1
' Make sure to add the "\" to the end
ListFolders Path & FolderList(i) & "\"
Next
End Sub