Macro open an excel file based on the latest date found in filename

moltenmagma

New Member
Joined
Aug 20, 2009
Messages
46
Hi,

I am faced with yet another challenge. :confused:

In my directory folder, I may have an excel files saved as

1. Test 10.08.09.xls
2. Test 20.08.09.xls
3. Test 25.08.09.xls

In Worksheet A, I would like to have a button/hyperlink that will open the above excel file that has the latest data. For instance, in this case, the file that needs to be open is Test 25.08.09.xls

Thank you very much in advance
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This macro will ask for a starting date and try to open the book with THAT date, failing, it will begin to search backwards from that date until it finds one matching the file name/format you have described. Be sure to edit ThePath.

Rich (BB code):
Sub OpenLatest()
'Opens a sheet based on date entry, searches backward til it finds a matching date
Dim TheString As String, TheDate As Date
Dim ThePath As String, TheBook As Workbook
Set TheBook = ThisWorkbook

ThePath = "C:\Documents and Settings\All Users\Documents\My Documents\Excel Tips\Test\"
TheString = Application.InputBox("Enter starting search date:", Title:="Beginning Search Date")
    
    If IsDate(TheString) Then
        TheDate = DateValue(TheString)
    Else
        MsgBox "Invalid date"
        Exit Sub
    End If
   
TheString = "Test " & WorksheetFunction.Text(TheDate, "DD.MM.YY") & ".xls"

On Error Resume Next
Do
Workbooks.Open ThePath & TheString

    If Err <> 0 Then
        TheDate = TheDate - 1
        TheString = "Test " & WorksheetFunction.Text(TheDate, "DD.MM.YY") & ".xls"
        Err = 0
    Else
        On Error GoTo 0
        Exit Do
    End If
Loop Until ActiveWorkbook.Name <> ThisWorkbook.Name
    
MsgBox "Lastest data found in sheet " & TheString
End Sub
I suppose we could make it just start searching from today's date backward, but your sample seemed to indicate some file dates may be in the future, thought it was best to just let you pick a starting date.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,991
Messages
6,175,820
Members
452,672
Latest member
missbanana

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