Macro to open all Excel Files in Directory and Sub-Directory

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I would like a macro to allow me to open up all excel files in directory and sub-directory C:\import that contains Br (P) in the file name

Your assistance is most appreciated
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Wigi

I have several files in directory c:\pull as well as several sub-folders in this directory starting with TB for eg TBBR1, TBBr2 etc

What I want to do is to open up all the XLS files that contains (p) in the file name for eg Br1 ACCNTS (p).xls

I have tried to amend the code per the link which you kindly provided, but no files are opened
,
I think it may have to do with the following code below


Code:
 Set fldStart = fso.GetFolder("C:\Pull") ' <-- use your FileDialog code here


See full code below

Your assistance in resolving this is most appreciated

Mask = "(p)*.xls"
Code:
 Option Explicit

Sub Open_Files_Pull()
    Dim fso As Object 'FileSystemObject
    Dim fldStart As Object 'Folder
    Dim fld As Object 'Folder
    Dim fl As Object 'File
    Dim Mask As String

    Set fso = CreateObject("scripting.FileSystemObject") ' late binding
    'Set fso = New FileSystemObject 'or use early binding (also replace Object types)

    Set fldStart = fso.GetFolder("C:\Pull") ' <-- use your FileDialog code here

    Mask = "(p)*.xls"
    Debug.Print fldStart.Path & "\"
    ListFiles fldStart, Mask
    For Each fld In fldStart.SubFolders
        ListFiles fld, Mask
        ListFolders fld, Mask
    Next
End Sub


Sub ListFolders(fldStart As Object, Mask As String)
    Dim fld As Object 'Folder
    For Each fld In fldStart.SubFolders
        Debug.Print fld.Path & "\"
        ListFiles fld, Mask
        ListFolders fld, Mask
    Next

End Sub

Sub ListFiles(fld As Object, Mask As String)
    Dim fl As Object 'File
    For Each fl In fld.Files
        If fl.Name Like Mask Then
            Debug.Print fld.Path & "\" & fl.Name
        End If
    Next
End Sub
 
Upvote 0
Hi Wigi

Have you had a chance to check my code?

Howard
 
Upvote 0
Hello

Thanks for asking, but unfortunately no, not had the time yet.
 
Upvote 0
Hi Wigi

No Problem-When you have a chance, it will be appreciated if you can have a look at my code

Howard
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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