macro to find latest created file in folder

poolf01

Board Regular
Joined
Apr 24, 2007
Messages
212
Once a week a dataset will be saved within a specified folder called "Balanced Score Card" on a shared drive. I want a macro that will enable the user to find the latest file by searching for the latest CREATED Date (not modified date) within this folder, open the file and paste the data into an Excel model.

Does anyone know of any code I can use to search for a file with the latest created date?

Many thanks
Faye
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I suggest taking the most recently created document, naming it blahblahCURRENT.xls and placing that in the source filename area below.

Hope this helps :)

Code:
Sub Copy()
    Dim PathToFile As String, _
    NameOfFile As String, _
    wbTarget As Workbook, _
    CloseIt As Boolean

' This macro will copy data from another workbook to another.
Application.DisplayAlerts = False
Application.ScreenUpdating = True 'Allows user to see what is happening.
Workbooks.Open Filename:="C:\Documents and Settings\Administrator\My Documents\Source.xls" 'Target source data file
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("A1:S" & LastRow).Copy  'copy the latest data (obviously adapt your range)
ActiveWindow.Close 'closes the workbook

Set wbTarget = Workbooks.Open("C:\Documents and Settings\Administrator\My Documents\Recipientdata.xls")  'Recipient data file
Sheets("Metric_Data").Select 'copy the new data in to the signatures sheet
LastRow = Range("C" & Rows.Count).End(xlUp).Row 'copy the latest data (obviously adapt your range)
Range("A1:S" & LastRow).Select
ActiveSheet.Paste

     'If you need to run a macro in that workbook, change !Macro to your Macro's name.
     'Application.Run (wbTarget.Name & "!Macro")

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

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