Search code examples
excelvbaif-statementdateaddweekday

Save a file as the previous weekday's date


I need to save a file as the previous weekday's date. On Mondays, I need to save the file as Friday's date.

Here's what I have:

If FileDate = Weekday(Date, vbMonday) Then Format(DateAdd("d", -3, Date)) = "yyyymmdd" Else FileDate = Format(DateAdd("d", -1, Date), "yyyymmdd")

This is spitting out date-1 even on Mondays.


Solution

  • Give it a try like this. It will print the correct name of the file:

    Public Sub TestMe()
    
        Dim fileDate    As String
    
        If Weekday(Date, vbMonday) = 1 Then
            fileDate = Format(DateAdd("d", -3, Date), "yyyymmdd")
        Else
            fileDate = Format(DateAdd("d", -1, Date), "yyyymmdd")
        End If
    
        Debug.Print fileDate
    
    End Sub