Excel VBA - How to Loop Through Folders and Subfolders

preview_player
Показать описание
Excel VBA Learn how to loop through folders and subfolders

The code used in this video:

Sub SetFolders()
Dim path As String
Dim folder As String
Dim row As Integer

path = "C:\Weekly Reports\"
folder = Dir(path, vbDirectory)
row = 1

Do While folder ""
If folder "." And folder ".." Then
If (GetAttr(path & folder) And vbDirectory) = vbDirectory Then
Cells(row, 1) = path & folder
row = row + 1
End If
End If
folder = Dir()
Loop

End Sub
Рекомендации по теме
Комментарии
Автор

My new task in the company was to find out, how the VBA folder search works. I spended all day and always saw some partial solutions... And now finally - everything I was looking for in one short code (or at least on the channel easy to find).
I love you, habibaty!
And to leave a code directly in the description - thank you as well!

vladislavspisligins
Автор

Thanks for the video...can u please explain why there is . And ..

tknaved
Автор

How to make automatic excel data base folders

KiyaniPlateOfFlavour
Автор

Great content! I was wondering why you had type vbDirectory twice @4:10?

ahrorkuldashev
Автор

Hello! Thanks for that macro.
I have two questions:
1) when you make the if statement to see if it's a directory, why do you have to use "and vbdirectory"? Isn't it enough with just the equal sign?

2) why appear the "." And the ".."
What do they represent?
Thanks :)

MagnusAnand
Автор

Hello,

Thank you for the code. How would I get excel to open everything in a folder and subfolders?

alisonchow
Автор

and how to get through the sub folders toi get the entire structure?

AniManuSCh