Looping thru a folder with FSO

upobDaPlaya
upobDaPlaya used Ask the Experts™
on
How do I loop thru a folder called October to load all the files found within the folder.

I know what to do with the file once I locate the file - I have code to import the file.  I am somewhat familiar with the FileSystem Object, but I am struggling with:

Once I identify the folder how do I proceed to loop thru to take the next file until empty ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
The following code will give you an idea about how to loop through the files in a folder...

'Reference to Microsoft Scripting RunTime Library must be added
Sub FsoExample()
Dim fso As FileSystemObject
Dim srcFolder As Folder
Dim xlFile As File
Dim FolderPath As String

FolderPath = "C:\Test"      'Source Folder Path
Set fso = New FileSystemObject

Set srcFolder = fso.GetFolder(FolderPath)   'Setting Source Folder

'Looping through all the files in the source folder
For Each xlFile In srcFolder.Files
    'Checking if the file in the source folder is an Excel file
    If fso.GetExtensionName(xlFile) = "xlsx" Then 'If the Excel file has .xlsx extension
        'Do whatever you want to do with that file here
        'Say you want to open the file then try this...
        Dim wb As Workbook
        Set wb = Workbooks.Open(xlFile)
    End If
Next xlFile
End Sub

Open in new window

Author

Commented:
Much appreciated..Exactly what I needed to get started
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial