简体   繁体   中英

Excel formula for previous weekday

I have a macro in which asks user for a date and it opens the file corresponding to the date and performs some operation. However, instead of entering the date manually I am trying to write an excel function to calculate the previous weekday. This is because the date I usually enter corresponds to the previous weekday.

For example, if today is Monday, I want last week's Friday date- and for the rest of the weekdays upto Friday, it's just the previous day. There won't be files corresponding to weekends.

How can I do this ?

=WORKDAY(TODAY(),-1)

This returns the serial number for the day in Excel, which can just be formatted in the cell.

See here for the full explanation of the function.

Try this in Excel 2010 or later:

=IF ( WEEKDAY (A1, 16) > 3 , A1-1 , A1 - WEEKDAY (A1, 16) )

Adding 16 as the second parameter of WEEKDAY sets Saturday = 1.

In VBA:

Public Function PreviousWeekDay(InputDate As Date) As Integer
    Dim Result As Integer
    Result = Weekday(InputDate, vbMonday)
    If Result = 6 Or Result = 7 Or Result = 1 Then
        Result = 5
    Else
        Result = Result - 1
    End If
    PreviousWeekDay = Result
End Function

Returns 1 = Monday, 2 = Tuesday etc.

=IF(WEEKDAY(NOW()-2)>5,NOW()-WEEKDAY(NOW()-6),NOW()-1)

然后只需格式化单元格,但您希望它显示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM