我从网上拉了一些代码来打开文件夹中的最新文件,这似乎工作得很好。然而,在代码的后面,我添加了一行附加行来设置最近打开的同一文件。尝试此操作时,工作簿 subscipt 超出范围,我认为这与语法有关?可能需要在工作簿名称中添加额外的引号,有什么想法吗?
“path”和“latestFile”变量似乎正确读取
Dim path$, file$, latestFile$
Dim LatestDate As Date, LMD As Date
Dim D As worksheet, dash As worksheet
'open latest file
path = "R:\Dept\"
If Right(path, 1) <> "\" Then path = path & "\"
file = Dir(path & "*.xls", vbNormal)
If Len(file) = 0 Then
MsgBox "No files were found in the folder", vbExclamation
End If
Do While Len(file) > 0
LMD = FileDateTime(path & file)
If LMD > LatestDate Then
latestFile = file
LatestDate = LMD
End If
file = Dir
Loop
Application.DisplayAlerts = False
Workbooks.Open path & latestFile
Application.DisplayAlerts = True
Set dash = Workbooks("dashboard.xlsm").Worksheets("D data")
Set D = Workbooks(path & latestFile).Worksheets("D Data") 'error here
只需使用名称:
Set D = Workbooks(latestFile).Worksheets("D Data")
但这样做更干净:
Application.DisplayAlerts = False
Set D = Workbooks.Open(path & latestFile).Worksheets("D Data") '<< edit
Application.DisplayAlerts = True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)