我有两个表 -CNSTimeVariance
一个工作簿WorseCase
中的表和一个名为的文件中的表Financial Model v12.xlsx
,我在目录中找到并打开它作为最近的文件。与此文件关联的变量是targetFilename
. 问题是这个文件名会不时改变,我想让它动态化。如何将Financial Model v12.xlsx
以下代码最后一行中给出的 xlookup 公式替换为targetFilename
?
SUB Xlookup()
Sheets("CNS Time Total").Select
Dim CNSTimeVariance As ListObject
Set CNSTimeVariance = ActiveSheet.ListObjects("CNSTimeVariance")
Dim DWB As Workbook
Set DWB = ActiveWorkbook
Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim targetFilename As String
Dim dteFile As Date
Const myDir As String = "C:\My Desktop Folders\Edge\7. Financial Models\"
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
targetFilename = objFile.Name
End If
Next objFile
MsgBox targetFilename
Workbooks.Open (myDir & targetFilename)
'Set FileSys = Nothing
'Set myFolder = Nothing
DWB.Activate
Range("CNSTimeVariance[P Hours]").FormulaR1C1 = _
"=XLOOKUP(CNSTimeVariance[@Helper],'Financial Model v12.xlsx'!WorseCase[Helper],'Financial Model v12.xlsx'!WorseCase[P Hours],""Not Found"")"
End Sub