Option Explicit
' This worksheet is read out
Const strSheetQ As String = "Sheet1"
' This worksheet is in the file with this code
Const strSheetZ As String = "Sheet1"
Public Sub Files_Read()
Dim intCalc As Integer
Dim strDir As String
Dim objFSO As Object
Dim objDir As Object
On Error GoTo Fin
With Application
.ScreenUpdating = False
.AskToUpdateLinks = False
.EnableEvents = False
intCalc = .Calculation
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With
Set objFSO = CreateObject("Scripting.FileSystemObject")
' File in the same folder as evaluation files
' strDir = ThisWorkbook.Path & "\"
' Fixed folder specified
strDir = "C:\Project\"
strDir = IIf(Right(strDir, 1) <> "\", strDir & "\", strDir)
Set objDir = objFSO.GetFolder(strDir)
With ThisWorkbook.Worksheets(strSheetZ)
' Delete everything from row 2 down
.Rows("2:" & .Rows.Count).ClearContents
' dirInfo objDir, "*.xls*", True ' with subfolders
dirInfo objDir, "*.xls*" ' without subfolders
' Convert formulas to values
.UsedRange.Value = .UsedRange.Value
End With
Fin:
With Application
.Goto (ThisWorkbook.Worksheets(strSheetZ).Range("A1")), True
.ScreenUpdating = True
.AskToUpdateLinks = True
.EnableEvents = True
.Calculation = intCalc
.DisplayAlerts = True
End With
Set objDir = Nothing
Set objFSO = Nothing
End Sub
Public Sub dirInfo(ByVal objCurrentDir As Object, ByVal strName As String, _
Optional ByVal blnTMP As Boolean = False)
Dim objWorkbook As Workbook
Dim strFormula As String
Dim strRange As String
Dim lngLastRow As Long
Dim arrCell As Variant
Dim intTMP As Integer
Dim varTMP As Variant
arrCell = Array("C6", "C9", "F11", "C16", _
"C22", "H93", "H109", "J62")
For Each varTMP In objCurrentDir.Files
If varTMP.Name Like strName And varTMP.Name <> _
ThisWorkbook.Name And Left(varTMP.Name, 1) <> "~" Then
With ThisWorkbook.Worksheets(strSheetZ)
lngLastRow = IIf(Len(.Cells(.Rows.Count, 1)), _
.Rows.Count, .Cells(.Rows.Count, 1).End(xlUp).Row) + 1
For intTMP = 1 To 8
strRange = arrCell(intTMP - 1)
strRange = Range(strRange).Address(RowAbsolute:=True, _
ColumnAbsolute:=True, ReferenceStyle:=xlR1C1)
'.Cells(lngLastRow, 1).Value = varTMP.Path ' with Path
'.Cells(lngLastRow, 1).Value = varTMP.Name ' only Filename
.Cells(lngLastRow, intTMP).Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, _
"\") + 1) & "]" & _
strSheetQ & "'!" & strRange
' If the file name should be (with or without path)
' in the first column, then take these lines
'.Cells(lngLastRow, intTMP + 1).Formula = "='" & Mid(varTMP.Path, 1, _
InStrRev(varTMP.Path, "\")) & "[" & _
Mid(varTMP.Path, InStrRev(varTMP.Path, _
"\") + 1) & "]" & _
strSheetQ & "'!" & strRange
Next intTMP
End With
End If
Next varTMP
If blnTMP = True Then
For Each varTMP In objCurrentDir.SubFolders
dirInfo varTMP, strName, blnTMP
Next varTMP
End If
Set objWorkbook = Nothing
End Sub