Knowing how to extract Excel files from multiple folders using Visual Basic for applications (VBA) can save you time when you need to search your computer for Excel files to store them in one folder. VBA can be used in Microsoft Office applications such as Excel to automate routine tasks such as searching for files in folders. The best way to search for files in multiple folders is by using recursive programming. Recursive programming is used when you need a function to call itself from within the function.
Launch Microsoft Office Excel, click "Developer," and click "Visual Basic" to launch the VB Editor. Click the "Insert" menu and click "Module" to insert a new code module.
Type the following to create a new sub-procedure:
Dim col Files As New Collection
Copy and paste the following to create your variables:
Define the path where you want to copy the Excel files found:
Add the following code to call the function that will find the Excel files for extraction:
Add the following code to copy the files found to the path defined in Step 3:
Define the "RecursiveDir" function to search the folders and sub-folders for Excel files:
Public Function RecursiveDir(col Files As Collection, _
strFolder = TrailingSlash(strFolder)
Create the following function to add or remove the "\" from the folder paths:
Public Function TrailingSlash(strFolder As String) As String
Click inside the "extractExcelFiles" sub-procedure and click "F5" to run the program.