Changing from referencing today's date to latest date

ahuang3433

New Member
Joined
Jan 24, 2017
Messages
39
I have this code where I hyperlink based on the today's date. Is there a way to make it look for the latest date just incase the a new file was not ran for the day?

VBA Code:
Dim lastRow As Long
Dim myPath As String, fileName As String


myPath = "Z:\Other\PjM_Documents\ESR Material Planning\Material Readiness\"
lastRow = Range("A" & Rows.count).End(xlUp).Row

For i = 2 To lastRow

    fileName = myPath & Range("A" & i).Value & " " & Format(Date, "MM-DD-YY") & "*.xlsm"
    
    If Len(Dir(fileName)) <> 0 Then
    
        ActiveSheet.Hyperlinks.Add Range("A" & i), myPath & Dir(fileName)
    
    End If
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Untested, but maybe something like this:

VBA Code:
Dim lastRow As Long
Dim myPath As String, fileName As String, i as Long, j as Long

myPath = "Z:\Other\PjM_Documents\ESR Material Planning\Material Readiness\"
lastRow = Range("A" & Rows.count).End(xlUp).Row

For i = 2 To lastRow
    For j = 0 to 5
        fileName = myPath & Range("A" & i).Value & " " & Format(Date - j, "MM-DD-YY") & "*.xlsm"
        If Len(Dir(fileName)) <> 0 Then Exit For
    Next j
    If j = 6 Then
        MsgBox "Could not find a good file on row " & i
    Else
        ActiveSheet.Hyperlinks.Add Range("A" & i), myPath & Dir(fileName)
    EndIf
Next i

This will check the last 5 days for a matching file name.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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