Pulling the most recent file in a folder

davidhall80

Well-known Member
Joined
Jul 8, 2006
Messages
663
I have a folder called 'Refresh'. Every week a new file is dropped into that folder. Instead of me placing that new file into my workbook, i would like my macro to just grab it. Is there a way for VB to grab the most recently created file in my 'Refresh' Folder.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I'm not sure but I think there is no "normal" VBA function that covers file size. If you can change the filename to a date it would be really easy to fix it however.
 
Upvote 0
Hi,

try this
This will open the most recent file with filename containing "John" in the folder "invoices".
Code:
Sub Test()
    With Application.FileSearch
        .NewSearch
        .LookIn = "D:\bus\acc\invoices" 'no backslash
        .Filename = "*John*.xls"
        .SearchSubFolders = True
        If .Execute(msoSortByLastModified, msoSortOrderDescending, True) > 0 Then
            Workbooks.Open .FoundFiles(1), xlWindows
        End If
    End With
End Sub
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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