open last saved excel file in folder and subfolder / vba excel 2010

RHB1987

New Member
Joined
Dec 9, 2010
Messages
34
Hello,

I have been searching for many hours for a VBA-code in Excel for 2010 for the following.

I want to open the last saved closed Excel-file that can be found in a folder and its subfolders. It has to open just one file in the folder or in one of its subfolders.
It should find and open e.g. example.xls in folder "example\folder1\folder2". Because I use different Excel files during the day and closing them, vba should open a file two hours later called later.xls in folder "example\folder3".
I found several codes on the internet, but many do not work in Excel 2010.

The code below works if you want to open a file in a specific folder, unfortunately the code does not search the subfolders. That is the most important part for me. Also it should only find *.xls (and/or xlsx.) If one or two filetypes could be specified, that would be great.
It would be very nice if someone could alter the code below, but this is not necessary, if you have an entirely different approach that is also ok.
But I know the code below works and many other codes on the internet do not for some reason.
For example, the Application.FileSearch does not work in 2010.

The code I have is from http://www.mrexcel.com/forum/showth...en-the-most-recently-created-file-in-a-folder . Many thanks to Iozzablake for the code.

Code:
Sub GetMostRecentFile()
    
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
    Const myDir As String = "c:\Refresh"
    
    '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 FileSys = Nothing
    Set myFolder = Nothing
End Sub

I hope there is someone who can alter this code. I have tried myself for some hours, but unfortunately I was unsuccessful.

Sincerely,
Richard
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
With some modifications I can get your code to work in Excel 2007, including in sub-folders:
Code:
Sub GetMostRecentFile()
    Dim FileSys As Object
    Dim objFile As Object
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
'Set const on next line to your folder path
    Const myDir As String = "C:\Users\Joe\Documents\Visual Basic for Excel"
    
    'set up filesys objects
    Set FileSys = CreateObject("Scripting.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
        'Debug.Print objFile.Name
        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
    Workbooks.Open myDir & Application.PathSeparator & strFilename
            
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub
 
Last edited:
Upvote 0
Hello JoeMo

I do not see where you search subfolders. Can you show indicate where your code does this?
 
Upvote 0
Hi wigi,

thank you for your postings. Unfortunately nothing yet.
I hope someone will have a good solution for this.

Sincerely,
Richard
 
Last edited:
Upvote 0
Hello JoeMo

I do not see where you search subfolders. Can you show indicate where your code does this?
I ran a quick test (apparently too quick!) and thought I was searching subfolders, but you are correct, the code I posted shows no evidence of this.
 
Upvote 0
I ran a quick test (apparently too quick!) and thought I was searching subfolders, but you are correct, the code I posted shows no evidence of this.

Hi Joe, it was a good try.

Maybe it is helpful if I sum up the changes that need to be made to the code.
I will use the altered code from Joe because it now opens only .xls-files.

Code:
Sub GetMostRecentFile()
    Dim FileSys As Object
    Dim objFile As Object
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
'Set const on next line to your folder path
    Const myDir As String = "C:\Users\Joe\Documents\Visual Basic for Excel"
    
    'set up filesys objects
    Set FileSys = CreateObject("Scripting.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
        'Debug.Print objFile.Name
        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
    Workbooks.Open myDir & Application.PathSeparator & strFilename
            
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub

Changes that need to be made:
  • search subfolders
  • open last-saved .xls-file, not last modified .xls-file

The difference between the last modified file and the last saved file is that the saved file is new in the folder or in the subfolders. If it finds the last modified file it only finds existing files that were modified (and saved offcourse) but not new files.

Anyone?
 
Upvote 0
Hi,

I have a code now that I have altered, and it works very good if you are just looking for the last modified .xls-file in a folder and its subfolders.
However, it still does not find the last-saved file in a folder and its subfolders. I mixed two codes together with a little alteration here and there.

If there is someone who can alter the code so it will open the last-saved file that would be very great.

HERE IS THE CODE, BUT IT IS NOT YET AS IT IS SUPPOSED TO BE:

Code:
Sub Finding_Last_Modified_File()
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim fdr As Scripting.Folder
Dim target As Scripting.file
Dim fls As Scripting.Folders
Dim strFile As String
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("YourFolderHere")
Set fls = fol.SubFolders
dteFile = DateSerial(1900, 1, 1)
 On Error Resume Next
For Each fdr In fls
  For Each target In fdr.files
  
        Debug.Print target.DateLastModified
        If InStr(1, target, ".xls") > 0 Then
            If target.DateLastModified > dteFile Then
                dteFile = target.DateLastModified
                strFile = target
            End If
        End If
  Next target
Next fdr
  Workbooks.Open strFile
Set fso = Nothing
Set fol = Nothing
Set fls = Nothing
End Sub

Anyone, please?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top