Opening multiple files

nikneven

Board Regular
Joined
Mar 20, 2006
Messages
117
I was wondering if there was a way to write a macro so that it goes to a folder ( G:\2007\9-07 ) and then opens all excel files in that folder and its sub folders that had the word Payroll in the name?
 

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
Code:
Private myList
Private n As Long

Sub SearchFiles()
     ListAllBooks "G:\2007\9-07", "*.xls", "*Payroll*"
     MsgBox IIf(n > 0, Join(myList,vbLf), "Not found")
     If n > 0 Then OpenFiles
End Sub

Private Sub OpenFiles()
Dim i As Long
For i = 1 To UBound(myList)
     Workbooks.Open(myList(i))
Next
End Sub

Private Sub ListAllBooks(myDir As String, myFileName As String, subName As String)
Dim fso As Object, myFile As Object, myFolder As Object
Set fso = CreateObject("FileSystemObject")
For Each myFile In fso.GetFoler(myDir).Files
     If myFile.Name Like myFileName Then
          n = n + 1 : ReDim Preserve myList(1 To n)
          myList(n) = myDir & "\" & myFile.Name
     End If
Next
For Each myFolder In fso.GetFoler(myDir).Subfolders
     If myFolder.Name Like subName Then
          ListAllBooks myDir & "\" & myFolder.Name, myFileName, subName
     End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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