open the newest excel file in a folder based on modified date

dulitul

Board Regular
Joined
Jan 19, 2013
Messages
193
Hello,

I want to open the second newest excel file in a folder baed on creation date, copy sheet1 from it and paste it into the newest excel file in the same folder. Can you please help?

Thank you
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
not tested
Code:
Sub LastUpdatedFile2()
        Set fs = CreateObject("Scripting.FileSystemObject")
        fpath = "C:\test"
        Set f = fs.Getfolder(fpath)
        Set NFile = f.Files
        For Each pf1 In f.Files
            If pf1.Name = "" Then Exit Sub
            date3 = pf1.DateLastModified
            If date3 > MaxData Then
                FpathName = pf1
                MaxData = date3
            End If
        Next
        For Each pf1 In f.Files
            If pf1.Name = "" Then Exit Sub
            date3 = pf1.DateLastModified
            If date3 > Max2 And date3 < MaxData Then
                Name2 = pf1
                Max2 = date3
            End If
        Next
        Set book2 = Workbooks.Open(Name2)
        Set book1 = Workbooks.Open(FpathName)
        book2.Sheets(1).copy  After:=book1.Sheets(book1.Sheets.count)
        book2.close
        book1.close true
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,553
Messages
6,125,483
Members
449,233
Latest member
Deardevil

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