ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   calculate weekdays only (https://www.excelbanter.com/excel-programming/310820-re-calculate-weekdays-only.html)

Ron Rosenfeld

calculate weekdays only
 
On Tue, 21 Sep 2004 13:55:02 -0700, "benb"
wrote:

I am looking for code to insert yesterday's date if yesterday was a weekday,
and to use the previous Friday's date otherwise (if yesterday was Sunday).
Anyone know how to do this? Thanks.


Some more thoughts:

If you don't have/don't want to have the ATP installed, I believe this formula
will work:

=A1-1-WEEKDAY(A1)*(WEEKDAY(A1)<3)

If you want it as a UDF, you can implement the same function in code:

=============
Function PrevWD(dt As Date) As Date
PrevWD = dt - 1 + Weekday(dt) * (Weekday(dt) < 3)
End Function
=============

Note that in code you have to reverse the sign of the weekend adjustment due to
the way that VBA handles booleans.


--ron


All times are GMT +1. The time now is 03:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com