Macro to extract Excel files within sub-folder in a main folder

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have a folder called C:\downloads

I downloaded a zip file and when unzipping the file, it created a sub-folder "Case Studies" and then for each individual workbook another sub-folder was created and within each of the sub-solders there are the excel workbooks



I would like a macro to extract excel workbooks from all the sub-folders within C:\downloads and paste them in C:\downloads


It would be appreciated if someone could assist me
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this.
Code:
Option Explicit
Dim FSO As Object

Sub GetAllDownloadExcelFiles()
Dim strStartFolder As String

    strStartFolder = "C:\test"

    Set FSO = CreateObject("Scripting.FileSystemObject")

    MoveFiles FSO.GetFolder(strStartFolder)

End Sub

Sub MoveFiles(Folder)
Dim fld As Object
Dim fl As Object

    For Each fld In Folder.SubFolders
        MoveFiles fld
    Next

    For Each fl In Folder.Files
        If fl.Name Like "*xls*" Then
            Debug.Print fl.Name
            FSO.MoveFile fl.Path, "C:\TestDownload\" & fl.Name
        End If
    Next

End Sub
 
Upvote 0
Thanks for the help


I have amended strStartFolder = "C:\test" to my folder

What do I change code below to ?


Code:
 FSO.MoveFile fl.Path, "C:\TestDownload\" & fl.Name [/code[
 
Upvote 0
You need to change "C:\TestDownload" to the path for the folder where you want to move the files to.
 
Upvote 0
Hi Norie

When the files were unzipped they were extracted in C:\Users\Downloads\Excel Case Studies and then within the folder Excel Case Studies there were sub-folders series 01, series 02, series 03 etc

I tried to amend your code as follows but I get path not found

Code:
 strStartFolder = "C:\Users\Downloads\Excel Case Studies\series*.*\"

I want the files extracted to


Code:
 FSO.MoveFile fl.Path, "C:\Users\Downloads" & fl.Name


It would be appreciated if you can assist me
 
Last edited:
Upvote 0
can you not just do a windows explorer search in the downloads folder of .xls files, select all results, and then cut paste them? or do you specifically need a macro?
 
Upvote 0
The code I posted will find Excel files in the folder specified in strStartFolder and all the sub-folders of that folder, so no need for 'series*.*'.
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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