You can use the DateAdd
function in VBA to subtract a day, like this:
sFilename = "\\y:cash " & Format(DateAdd("d", -1, Date), "dd") & Format(Date, "mm") & Right(Year(Date), 2) & ".xlsx"
And you dont need the CStr function, as the format and right functions already return strings.
But what about the first day of the month?
It would be better to do it like this:
sFilename = "\\y:cash " & Format(DateAdd("d", -1, Date), "ddmmyy") & ".xlsx"
Alternatively, you can have a function that would return an empty string if no file was found, or return the filename of the most recent file based on your criteria, like this:
Public Function GetMostRecentFileByDate(dtStart As Date, sPath As String, sPrefix As String, sExt As String, sFormat As String) As String
Dim nDay As Integer
Dim sFilename As String
Dim dtDate As Date
Dim sFull As String
dtDate = dtStart
For nDay = -1 To -7 Step -1
sFilename = sPrefix & Format(dtDate, sFormat) & "." & sExt
sFull = sPath & "\" & sFilename
If Dir(sFull) <> "" Then
GetMostRecentFileByDate = sFull
Exit Function
End If
dtDate = DateAdd("d", -1, dtDate)
Next
End Function
Usage:
sFullName = GetMostRecentFileByDate("03 Sept 2019", "\\y:", "cash ", "xlsx", "ddmmyy")
If sFullName <> "" Then
' Do Something With It
End If