Option Explicit
Public strSourceFldr As String
Public EachFile As Object
Public objFSO As Object
Public objFolder As Object
Public objFile As Object
Public strSheetName As String
Public strSrcCell1 As String
Public strSrcCell2 As String
Public strSrcCell3 As String
Public strSrcCell4 As String
Public intStartCell As Integer
Sub DataCopy()
strSourceFldr = Worksheets(1).Cells(1, 14)
strSheetName = "Sheet1"
strSrcCell1 = "B2"
strSrcCell2 = "C2"
strSrcCell3 = "D4"
strSrcCell4 = "F3"
intStartCell = 2
Set objFSO = CreateObject("Scripting.Filesystemobject")
Set objFolder = objFSO.GetFolder(strSourceFldr)
For Each EachFile In objFolder.Files
If LCase(objFSO.GetExtensionName(EachFile)) = "xls" Then
ProcessFile EachFile
End If
Next
ProcessSubFolder objFSO.GetFolder(strSourceFldr)
End Sub
Sub ProcessFile(ByRef ThisFile As Object)
Dim Cell1, Cell2, Cell3, Cell4, WS
Dim strValidFile As String
strValidFile = "Sheet " & strSheetName & " not found"
Set objFile = objFSO.GetFile(ThisFile)
Workbooks.Open ThisFile
For Each WS In ActiveWorkbook.Worksheets
If WS.Name = strSheetName Then
strValidFile = ThisFile.Name
Cell1 = Range(strSrcCell1).Value
Cell2 = Range(strSrcCell2).Value
Cell3 = Range(strSrcCell3).Value
Cell4 = Range(strSrcCell4).Value
End If
Next
ActiveWorkbook.Close
Worksheets(1).Cells(intStartCell, 1) = strValidFile
Worksheets(1).Cells(intStartCell, 2) = Cell1
Worksheets(1).Cells(intStartCell, 3) = Cell2
Worksheets(1).Cells(intStartCell, 4) = Cell3
Worksheets(1).Cells(intStartCell, 5) = Cell4
Worksheets(1).Cells(intStartCell, 6) = ThisFile.Path
intStartCell = intStartCell + 1
End Sub
Sub ProcessSubFolder(ByRef ThisFolder As Object)
Dim SubFolder
For Each SubFolder In ThisFolder.SubFolders
Set objFolder = objFSO.GetFolder(SubFolder.Path)
For Each EachFile In objFolder.Files
If LCase(objFSO.GetExtensionName(EachFile)) = "xls" Then
ProcessFile EachFile
End If
Next
ProcessSubFolder objFolder
Next
End Sub