Option Explicit
Sub ScanSuppliers()
Dim X
Dim strPath$, strFile$, strSuppName$, strThisFile$, strExt$, strTargetSheet$, strSuppFile$, strSuppID$
Dim strError$
Dim sglSuppAve!, intRedCount%, intRowCount%, intRowCountOriginal%
Dim wbTarget As Workbook, wbThisWB As Workbook
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.AskToUpdateLinks = False
strThisFile = ActiveWorkbook.Name
strTargetSheet = wksSupplier.Range("SuppSheetName").Value
strSuppFile = wksSupplier.Range("SuppSheetFile").Value
intRowCount = wksSupplier.Range("RowOne").Value
intRowCountOriginal = wksSupplier.Range("RowOne").Value
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
MsgBox "No folder chosen"
Exit Sub
End If
strPath = .SelectedItems(1) & "\"
End With
strPath = strPath
If strPath = "" Then
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
MsgBox "No folder chosen"
Exit Sub
End If
'Target File Extension (must include wildcard "*")
strExt = strSuppFile & "*.xls*"
strFile = Dir(strPath & strExt)
Set wbThisWB = ActiveWorkbook
On Error Resume Next
wksSupplier.Range("Data").ClearContents
On Error GoTo 0
'Loop through each Excel file in folder
Do While strFile <> ""
'Set variable equal to opened workbook
If strFile <> strThisFile Then
Set wbTarget = Workbooks.Open(Filename:=strPath & strFile)
If UCase(Left(strFile, Len(strSuppFile))) = UCase(strSuppFile) Then
strSuppID = Mid(strFile, Len(strSuppFile) + 1, InStrRev(strFile, ".") - Len(strSuppFile) - 1)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Get information from target workbook
On Error Resume Next
Sheets(strTargetSheet).Activate
If Err.Number <> 0 Then
On Error GoTo 0
strSuppName = "Not known"
sglSuppAve = 0
intRedCount = 0
strError = "Can't find sheet [" & strTargetSheet & "] in " & strFile
Else:
On Error GoTo 0
strSuppName = Cells(2, 3).Value
sglSuppAve = Cells(4, 3).Value
intRedCount = Cells(6, 3).Value
strError = ""
End If
'Paste into this workbook
wbThisWB.Activate
Sheets("Summary").Activate
Cells(intRowCount, 2).Value = strSuppName
Cells(intRowCount, 3).Value = sglSuppAve
Cells(intRowCount, 4).Value = intRedCount
Cells(intRowCount, 5).Value = strError
Application.ScreenUpdating = True
DoEvents
Application.ScreenUpdating = False
intRowCount = intRowCount + 1
End If
'Save and Close Workbook
wbTarget.Close SaveChanges:=False
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
End If
strFile = Dir
Loop
' If IntRowCount has incremented (i.e. 1 or more files found) then copy match formula down
If intRowCount <> intRowCountOriginal Then
Range("MyFormula").Copy
Range("PopulatedRows").Offset(0, -1).PasteSpecial (xlPasteFormulas)
End If
'reset system flags
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
Application.Calculation = xlCalculationAutomatic
Application.CutCopyMode = False
'Message Box when tasks are completed
If intRowCount <> intRowCountOriginal Then
MsgBox "All done " & (intRowCount - wksSupplier.Range("RowOne").Value) & " files processed"
Else
MsgBox "Could not find any files called 'Supplier HealthCheck - '+supplier name in the folder you chose"
End If
Cells(wksSupplier.Range("RowOne").Value, 3).Select
End Sub