Thread: Dating Problem
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Dating Problem

On Tue, 21 Jun 2005 03:29:03 -0700, "Gary's Student"
wrote:

I need a UDF that, given a date, returns the date of the Monday of that week.
For example, if the input was a value between 6/20/2005 and 6/26/2005, the
function would return 6/20/2005. If the input was 6/27/2005 the UDF should
return 6/27/2005.

All I can think of is a massive VLOOKUP table. There has got to be a
cleaner way.

Thank you in advance


What is the first day of your week?

If the first day of your week is Sunday, then:

=A1+2-WEEKDAY(A1)

If the first day of your week is Monday, then:

=A1+1-WEEKDAY(A1,2)

In VB:

Function FirstMon(dt As Date) As Date
FirstMon = dt + 2 - Weekday(dt, vbSunday)
End Function

or
Function FirstMon(dt As Date) As Date
FirstMon = dt + 1 - Weekday(dt, vbMonday)
End Function


--ron