Excel VBA - How to Loop Through Folders and Subfolders

  Рет қаралды 14,664

InAnOffice

InAnOffice

Күн бұрын

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

Пікірлер: 11
@vladislavspisligins1561
@vladislavspisligins1561 4 жыл бұрын
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!
@tknaved
@tknaved Жыл бұрын
Thanks for the video...can u please explain why there is . And ..
@ahrorkuldashev9603
@ahrorkuldashev9603 4 жыл бұрын
Great content! I was wondering why you had type vbDirectory twice @4:10?
@KiyaniPlateOfFlavour
@KiyaniPlateOfFlavour 3 жыл бұрын
How to make automatic excel data base folders
@MagnusAnand
@MagnusAnand 8 жыл бұрын
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 :)
@alisonchow9216
@alisonchow9216 8 жыл бұрын
Hello, Thank you for the code. How would I get excel to open everything in a folder and subfolders?
@torque6389
@torque6389 6 жыл бұрын
Wonderful
@AniManuSCh
@AniManuSCh 4 жыл бұрын
and how to get through the sub folders toi get the entire structure?
@InAnOfficeExcelVBA
@InAnOfficeExcelVBA 4 жыл бұрын
Hi, here we go: Sub SomeSub() Call GetFiles("C:\Weekly Reports\") End Sub Sub GetFiles(ByVal path As String) Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim folder As Object Set folder = fso.GetFolder(path) Dim subfolder As Object For Each subfolder In folder.SubFolders Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = subfolder.path GetFiles (subfolder.path) Next subfolder Set fso = Nothing Set folder = Nothing Set subfolder = Nothing End Sub
@AniManuSCh
@AniManuSCh 4 жыл бұрын
@@InAnOfficeExcelVBA Wow! Awesome, thanks, I guess I just have to do something similar for the files too, right
Loop Through All Files in a Folder Using VBA/Macros in Excel
13:41
Excel VBA Loop Thru Folder and Subfolders
10:36
The Excel Cave
Рет қаралды 6 М.
Wise Owl Answers - How do I unzip files using VBA?
13:25
WiseOwlTutorials
Рет қаралды 7 М.
Excel Macros & VBA - Tutorial for Beginners
50:20
Kevin Stratvert
Рет қаралды 1,3 МЛН
VBA Utility - How to create Folder Structure using Excel VBA
21:13
Tech Sessions
Рет қаралды 3,9 М.
How to Loop through Sub-Folders
18:21
Dinesh Kumar Takyar
Рет қаралды 10 М.
VBA to Loop Through Files in a Folder - Excel VBA Macros example
5:50
Excel Destination
Рет қаралды 6 М.