Gustav Brock
Gustav at cactus.dk
Thu Mar 30 01:33:52 CST 2006
Hi all
Ever had the need to find next Monday or Thursday whatever is closest? Or similar.
Here's how:
datNow = Date
datNext = DateAdd("d", 4 - Weekday(datNow, vbMonday) Mod 4, datNow)
The days to add from Monday to Wednesday is 3-2-1.
The days to add from Thursday to Sunday is 4-3-2-1.
Weekday(date, vbMonday) returns the weekday with Monday as weekday 1. Note that no weekday is zero.
The modulus 4 "resets" the weekday count to "start over" at Thursday at which the returned value is zero.
Thus the returned modified weekday for Monday to Thursday is 1-2-3-0-1-2-3.
The maximum days to add is 4 days.
Now, subtract the modified weekday from this to obtain the days to add for Monday to Sunday:
3-2-1-4-3-2-1.
/gustav