You don't say what you are doing with the folder name(s), but I found some code (at https://www.excelhowto.com/macros/ho...use-excel-vba/ and https://excelmacromastery.com/vba-di...VBA_Dictionary ) and modified it.
Right now the code asks you to pick a folder and loops through the path collecting all folders/sub folders. It found over 4400 folders (I picked a drive) in under 3 seconds (I didn't time it, but is was fast).
You could modify the "Select folder" code to search for a folder, if that is what you need.
Then it writes the found folders to the immediate window.
Instead of writing to the immediate window, you could modify the code to write to a table, then do what you need to do with the folders.
Code:
Sub ListAllFolders()
Dim MyPath As String, MyFolderName As String
Dim i As Integer, F As Boolean
Dim objShell As Object, objFolder As Object, AllFolders As Object
Dim key
Dim tmp()
On Error Resume Next
'************************
'Select folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "", 0, 0)
If Not objFolder Is Nothing Then
MyPath = objFolder.self.Path & "\"
Else
Exit Sub
'MyPath = "G:\BackUp\"
End If
Set objFolder = Nothing
Set objShell = Nothing
'************************
'Find all folders/sub folders
Set AllFolders = CreateObject("Scripting.Dictionary")
AllFolders.Add (MyPath), ""
i = 0
Do While i < AllFolders.Count
key = AllFolders.Keys
MyFolderName = Dir(key(i), vbDirectory)
Do While MyFolderName <> ""
If MyFolderName <> "." And MyFolderName <> ".." Then
If (GetAttr(key(i) & MyFolderName) And vbDirectory) = vbDirectory Then
AllFolders.Add (key(i) & MyFolderName & "\"), ""
End If
End If
MyFolderName = Dir
Loop
i = i + 1
Loop
'display the folders found in the immediate window
PrintContents AllFolders
Set AllFolders = Nothing
End Sub
'##########################################
Sub PrintContents(dict As Scripting.Dictionary)
Dim k As Variant
For Each k In dict.Keys
' Print key and value
Debug.Print k ' << could write to a table here
Next
End Sub
EDIT: the folder/sub folders includes the full path, so if you want just the folder name, you will have to parse the string to get just the folder name.