View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bill Manville Bill Manville is offline
external usenet poster
 
Posts: 473
Default anyone have any improvements for my Weeks Function?

Reesmacleod wrote:
I am a bit of a "hack" programmer and came up with this after days of
trial and error, and would love if any professional out there could let
me know if it is "hilarious" or "ingeniuos" or any improvements anyone
might have.


Firstly the code only works if your dates are formatted as m/d/y so it
typically won't work outside the USA.

My go would be

Function GetWeekNumber(DT As Date) As Integer
Dim FirstSunOfYear As Date
FirstSunOfYear = DateSerial(Year(DT),1,1) + 7 - _
WeekDay(DateSerial(Year(DT),1,1), vbMonday)
If DT < FirstSunOfYear Then ' end of last year
FirstSunOfYear = DateSerial(Year(DT)-1,1,1) + 7 - _
WeekDay(DateSerial(Year(DT)-1,1,1), vbMonday)
End If
GetWeekNumber = (DT - FirstSunOfYear) \ 7 + 1
End Function

You can call it from a worksheet with
=GetWeekNumber(TODAY())
or any other date as argument that you might want.

Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup