View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Calculate week from current day

On Fri, 4 Dec 2009 03:58:22 -0700, "Robert Crandal" wrote:

This is probably an easy question, but i'll ask anyhow:

One of my subroutines is passed the current date in "mm-dd-yy"
format. For example, it would pass in a value of "12-04-09"
for today's date.

I now need some code that will give me the Sunday to Saturday
date range that coincides with the current date. Therefore,
the code should give me a start date of "11-29-09" and end
date of "12-05-09" because "12-04-09" is part of that week.

thanx again


Here's one way to get those two dates:

================================
Option Explicit
Sub WeekRange()
Dim dt As Date
dt = #12/4/2009#
Debug.Print WeekDates(dt)(0), WeekDates(dt)(1)
End Sub
'------------------------------------------------
Function WeekDates(dt As Date) As Variant
Dim StartDt As Date, EndDt As Date
Dim sTemp(0 To 1)

StartDt = dt - Weekday(dt, vbMonday)
EndDt = dt + 7 - Weekday(dt, vbSunday)

sTemp(0) = StartDt
sTemp(1) = EndDt

WeekDates = sTemp
End Function
====================================
--ron