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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
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.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
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
 

Forum statistics

Threads
1,137,205
Messages
5,680,174
Members
419,887
Latest member
Vasokir

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
Top