VBA Loop through all files in all subfolders

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
938
Dear all

Rich (BB code):
Sub LopFolder()

    Dim fso As New FileSystemObject
    Dim f As Folder, sf As Folder
    Dim MyPath As String, MyFile As String, File As Workbook

    Set f = fso.GetFolder("E:\Da\")
    For Each sf In f.SubFolders
        MyFile = Dir(sf & "\" & "*.xls*") ''''''''''''''''Loop through all this files before goto next sf.
        MyFile = Dir()
    Next

End Sub
I am struggling no loop through all files first before next sf. Could someone help please.
I have tried this but not work
Rich (BB code):
For Each File in sf

Thank you very much
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this:

Code:
Sub LoopFolder()


    Dim fso As New FileSystemObject
    Dim f As Folder, sf As Folder
    Dim MyPath As String, MyFile As String, File As Workbook


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("C:\TestingResults\BBR\")
    For Each sf In f.SubFolders
[COLOR=#0000ff]        For Each f In sf.Files[/COLOR]
[COLOR=#0000ff]            If fso.GetExtensionName(f.Path) = "xls" Then[/COLOR]
[COLOR=#0000ff]                '' process here[/COLOR]
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        Next[/COLOR]
    Next


End Sub
 
Upvote 0
Try this:

Code:
Sub LoopFolder()


    Dim fso As New FileSystemObject
    Dim f As Folder, sf As Folder
    Dim MyPath As String, MyFile As String, File As Workbook


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("C:\TestingResults\BBR\")
    For Each sf In f.SubFolders
[COLOR=#0000ff]        For Each f In sf.Files[/COLOR]
[COLOR=#0000ff]            If fso.GetExtensionName(f.Path) = "xls" Then[/COLOR]
[COLOR=#0000ff]                '' process here[/COLOR]
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        Next[/COLOR]
    Next


End Sub
Thank you so much. I don't understand one thing. Could you please bright me up.
Rich (BB code):
Dim f As Folder
For Each f in sf.Files
It shd be like this.
Dim f as workbook or file. Then for each f(workbook or file) in sf.Files
 
Upvote 0
Ah I didn't notice your declaration part. Rewrote sub with variable declarations:

Code:
Sub LopFolder()


    Dim fso As New FileSystemObject
    Dim f As Folder, sf As Folder
[COLOR=#0000ff]    Dim ofile As File[/COLOR]
    Dim MyPath As String, MyFile As String, File As Workbook


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("C:\TestingResults\BBR\")
    For Each sf In f.SubFolders
[COLOR=#0000ff]        For Each ofile In sf.Files[/COLOR]
[COLOR=#0000ff]            If fso.GetExtensionName(ofile.Path) = "xls" Then[/COLOR]
[COLOR=#0000ff]                Debug.Print ofile.Name[/COLOR]
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        Next[/COLOR]
    Next


End Sub
 
Upvote 0
Ah I didn't notice your declaration part. Rewrote sub with variable declarations:

Code:
Sub LopFolder()


    Dim fso As New FileSystemObject
    Dim f As Folder, sf As Folder
[COLOR=#0000ff]    Dim ofile As File[/COLOR]
    Dim MyPath As String, MyFile As String, File As Workbook


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("C:\TestingResults\BBR\")
    For Each sf In f.SubFolders
[COLOR=#0000ff]        For Each ofile In sf.Files[/COLOR]
[COLOR=#0000ff]            If fso.GetExtensionName(ofile.Path) = "xls" Then[/COLOR]
[COLOR=#0000ff]                Debug.Print ofile.Name[/COLOR]
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        Next[/COLOR]
    Next


End Sub
Cool thank you very much
 
Upvote 0

Forum statistics

Threads
1,223,991
Messages
6,175,818
Members
452,672
Latest member
missbanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top