我試圖打開文件夾中最近的兩個excel文件,目前爲止我打開文件夾中的最新文件但我必須打開文件夾中的第二個最新文件。請參考以下代碼。請建議如何打開第二個最近的文件?VBA:如何在文件夾中打開最近的兩個excel文件
Sub findingdiff()
Dim FileSys, objFile, myFolder, c As Object
Dim wb1 As Workbook
Dim wb2 As Workbook
FolderName = ("C:\Users\ashokkumar.d\Desktop\Test\do\")
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set myFolder = FileSys.GetFolder(FolderName)
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If InStr(1, objFile.Name, ".xls") > 0 Then
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
End If
Next objFile
'opening of latest file in the folder
Set wb2 = Workbooks.Open(FolderName & Application.PathSeparator & strFilename)
End Sub
一旦你已經確定你可以簡單地再次運行循環,但最新的文件(如strFileNameNewest)有一個,如果只運行行語句 strFilename = objFile.Name 如果objFile.Name不是你已經存儲爲strFileNameNewest的那個 – AndyW