您好我正在尝试在文件夹中打开最新文件(修改日期)。代码使用循环来浏览文件并找到它所做的最新修改但是当使用' Workbooks.Open strFilename'打开文件时。它说文件(它已经被识别为最年轻的'文件找不到。这对我来说没有意义,因为错误消息说文件'测试年轻'测试年轻' 39; - 找不到文件名,但它在循环中清楚地找到了它。
Sub copynewdata()
Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dteFile As Date
Dim Ref As Object, CheckRefEnabled%
CheckRefEnabled = 0
With ThisWorkbook
For Each Ref In .VBProject.References
If Ref.Name = "Scripting" Then
CheckRefEnabled = 1
Exit For
End If
Next Ref
If CheckRefEnabled = 0 Then
.VBProject.References.AddFromGUID "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
End If
End With
'set path for files - change for your folder
Const myDir As String = "\\C:\Test"
'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)
'loop through each file and get date last modified. If largest date then store Filename
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
Next objFile
Workbooks.Open strFilename
'Set Source_Workbook = Workbooks(strFilename).Open(Target_Path)
Set FileSys = Nothing
Set myFolder = Nothing
End Sub
任何人都可以帮忙吗?
答案 0 :(得分:1)
在strFilename
中,您拥有文件的名称 - 但没有路径。将open-command更改为
Workbooks.Open myDir & "\" & strFilename