Option Explicit
Const TheSheet As String = 'PURCHASING PLAN'
Const TheAddress As String = '$C$72'
Sub TheSearcher()
Dim ThisBookPath As String, TabFilePathName() As Variant
Dim FileSearcher As FileSearch
Dim ThePath As String
Dim i As Integer
Set FileSearcher = Application.FileSearch
ThisBookPath = ThisWorkbook.Path
ThePath = ThisBookPath
With FileSearcher
.NewSearch
.Filename = '*.xls*'
.LookIn = ThePath
.SearchSubFolders = True
.Execute msoSortByFileName, msoSortOrderAscending
If .Execute > 0 Then
With .FoundFiles
For i = 1 To .Count
ReDim Preserve TabFilePathName(2, 0 To .Count - 1)
TabFilePathName(0, i - 1) = .Item(i)
TabFilePathName(1, i - 1) = Dir(.Item(i))
Next i
End With
Else
MsgBox 'Pas de Fichier trouvé dans ' & ThePath
Exit Sub
End If
End With
Set FileSearcher = Nothing
Range('A2').Resize(UBound(TabFilePathName, 2) + 1, UBound(TabFilePathName, 1)) = Application.Transpose(TabFilePathName)
TheFormulator
End Sub
Sub TheFormulator()
Dim Cell As Range
Dim TheFullPath As String
Dim ThePath As String
Dim TheFile As String
Dim TheFormula As String
For Each Cell In Range('A2:A' & Range('A5000').End(xlUp).Row)
TheFullPath = Cell.Text
TheFile = Cell.Offset(0, 1).Text
ThePath = Left(TheFullPath, Len(TheFullPath) - Len(TheFile))
TheFormula = '='' & ThePath & '[' & TheFile & ']' & TheSheet & ''!' & TheAddress
Cell.Offset(0, 2).Formula = TheFormula
Next
End Sub