Try this code in a standard module in your main workbook. Run the Create_Formula macro to create the =INDEX formula - which references the latest matching file in the same folder as the main workbook - in cell A2 of Sheet1. You could call this from the Workbook_Open event handler.
Code:
Public Sub Create_Formula()
Dim latestFileName As String
latestFileName = FindLatestFile(ThisWorkbook.Path & "\02. Field Quota Planning 2018 v*.xlsx")
If latestFileName <> "" Then
Workbooks.Open ThisWorkbook.Path & "\" & latestFileName
ThisWorkbook.Worksheets("Sheet1").Range("A2").Formula = "=INDEX('[" & latestFileName & "]All Reps'!$M:$M,MATCH(K4,'[" & latestFileName & "]All Reps'!$A:$A,0))"
Else
MsgBox "No files found matching '02. Field Quota Planning 2018 v*.xlsx' in " & ThisWorkbook.Path
End If
End Sub
Private Function FindLatestFile(fullFileSpec As String) As String
Dim folder As String, fileName As String
Dim latestFileDate As Date
FindLatestFile = ""
folder = Left(fullFileSpec, InStrRev(fullFileSpec, "\"))
latestFileDate = 0
fileName = Dir(fullFileSpec, vbNormal)
Do While fileName <> vbNullString
If FileDateTime(folder & fileName) > latestFileDate Then
FindLatestFile = fileName
latestFileDate = FileDateTime(folder & fileName)
End If
fileName = Dir
Loop
End Function
Thank you so much, this worked great.
We're trying to add an auto fill now. I will have a great deal of formulas in different columns referencing this file. That's why I wanted to not have to remember all the places its linked.
Our auto fill tries have not worked. If you have time can you help with ours below?
Public Sub Create_Formula()
Dim latestFileName As String
latestFileName = FindLatestFile("\\Slnas3\nas\lib-SL\Sales\sales_enablement\001 New Stuff\Comp Plans\2018\Quota Planning" & "\02. Field Quota Planning 2018 v*.xlsx")
lastRow = Range("S4").End(xlDown).Row
If latestFileName <> "" Then
Workbooks.Open "\\Slnas3\nas\lib-SL\Sales\sales_enablement\001 New Stuff\Comp Plans\2018\Quota Planning" & "" & latestFileName
ThisWorkbook.Worksheets("Detail").Range("T4").Formula = "=INDEX('[" & latestFileName & "]All Reps'!$M:$M,MATCH(K4,'[" & latestFileName & "]All Reps'!$A:$A,0))"
Else
MsgBox "No files found matching '02. Field Quota Planning 2018 v*.xlsx' in " & ThisWorkbook.Path
End If
Range("T4").AutoFill Destination:=Range(Range("T4"), Range("T" & lastRow))
End Sub