how to open latest date in file name

jason1000rr

New Member
Joined
Sep 2, 2016
Messages
14
Hi Everyone--
Looking for guidance to open file with the latest date within its name. Below is what I am working with. I typed "Latest Date" where in name it would go. Name is always recent to current month, but date is older. Any help would be much appreciated. Thank you

Fname = "\\global.corp.net\someplace\someplaceProcurement\IQR\IQR " & LatestDate & ".xlsx"
Set wbb = Workbooks.Open(Filename:=Fname, UpdateLinks:=False, Notify:=False)
For Each wb In Application.Workbooks


I used below for another file I used to work with and it worked for when it was current date. But, not able to figure out for latest date.

Dim curDate As String, Fname As String
curDate = Format(Date, "yyyy-mm-dd")
Dim wba As Workbook, wbb As Workbook, wbc As Workbook, wbd As Workbook, wbe As Workbook, wbf As Workbook, wb As Workbook
Fname = "Y:\Consumables\corporation\DMC\DMC - Planning & Materials\On Hand Reports\DMC Site\" & curDate & "_INV_R12_DMC_Onhandreport.xlsx"
Set wba = Workbooks.Open(Filename:=Fname, UpdateLinks:=False, Notify:=False)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe play with this.
 
Upvote 0
I am away from my PC so cannot test this
I think this will find the latest file based on the NAME of the file using your naming convention
Place both procedures in the same nodule
Amend file path and file name
Run TestFunction

VBA Code:
Sub TestFunction()
    Const fPath = "c:\folder\subfolder\etc"                     'amend
    Const fExt = "xlsx"
    Const fName = "name excluding date stamp"                   'amend
    MsgBox GetLatestFile(fPath, fName, fExt)

End Sub

Private Function GetLatestFile(xPath, xName, xExt)
    Dim f As String, latest As String
    f = Dir(xPath & "\" & xName & "*." & xExt)
    Do While Len(f) > 0
        If f Like xName & "*." & xExt Then latest = f
        f = Dir
    Loop
    GetLatestFile = latest
End Function
 
Upvote 0
Below is what I entered and worked for opening the file with latest date. Got one more issue I have been searching to resolve without a solution yet.
This excel file has 4 tabs. I need to select the tab that has a date format as the tab name (ex. 12262019). It is the only tab with a name as such.
And finally, need to close the below excel file once done. Could you please assist? Thank you

Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "\\global.corp.net\brea\Procurement\IQR"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
 
Upvote 0
As far as I know there is no way to identify a worksheet based on the formatting of its name.
A possible approach is to use a procedure that determines whether the first eight characters of the sheet name consist of numbers, for example ...
VBA Code:
Public Sub Example()

    Dim MyPath      As String
    Dim MyFile      As String
    Dim LatestFile  As String
    Dim LatestDate  As Date
    Dim LMD         As Date
    
                ' additional declarations
                Dim oWb         As Workbook
                Dim oWs         As Worksheet
                Dim lngTmp      As Long
            
    MyPath = "\\global.corp.net\brea\Procurement\IQR"
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If
    MyFile = Dir(MyPath & "*.gif", vbNormal)
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
    Do While Len(MyFile) > 0
        LMD = FileDateTime(MyPath & MyFile)
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        MyFile = Dir
    Loop
    
                ' additional code
                Set oWb = Workbooks.Open(MyPath & LatestFile)
                With oWb
                    For Each oWs In .Worksheets
                        On Error Resume Next
                        lngTmp = CLng(Left(oWs.Name, 8))
                        On Error GoTo 0
                        If lTmp <> 0 Then
                            Exit For
                        End If
                    Next

                    If Not oWs Is Nothing Then
                        With oWs
' -------------------
' your code goes here
' oWs points to your worksheet (no selection required)
' -------------------
                        End With
                    End If

                    ' closing the workbook we've just opened                    
                    .Close
                End With
                ' cleaning up
                Set oWs = Nothing
                Set oWb = Nothing
End Sub
 
Upvote 0
Beware! I made a typo in the above code (sorry) ...
Replace te For Each loop with the code below.

VBA Code:
                    For Each oWs In .Worksheets
                        On Error Resume Next
                        lngTmp = CLng(Left(oWs.Name, 8))
                        On Error GoTo 0
                        If lngTmp <> 0 Then
                            Exit For
                        End If
                    Next
 
Upvote 0
Beware! I made a typo in the above code (sorry) ...
Replace te For Each loop with the code below.

VBA Code:
                    For Each oWs In .Worksheets
                        On Error Resume Next
                        lngTmp = CLng(Left(oWs.Name, 8))
                        On Error GoTo 0
                        If lngTmp <> 0 Then
                            Exit For
                        End If
                    Next
Thank you. Couldn't get it to work. It doesn't give an error code, but doesn't change tabs either.
 
Upvote 0
i was thinking maybe something like if the sheet name has a 20 in it then activate. (just not sure how to do so)
 
Upvote 0
... but doesn't change tabs either.

You're right, but that's not necessary. The oWs already refers to the chosen sheet; if you insert a line in your code (at "your code goes here") like
.Select
the sheet (tab) will be selected.
 
Upvote 0

Forum statistics

Threads
1,215,177
Messages
6,123,475
Members
449,100
Latest member
sktz

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