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 Function for week number

On Tue, 13 Jan 2009 05:34:01 -0800, Gunnar Aronsen, SKM Market Predictor AS
<Gunnar Aronsen, SKM Market Predictor wrote:

The function below creates week numbers in many different forms.

Public Function WeekValue(Dato As Date, Optional FormatValue As String =
"w", Optional Invert As Boolean = False) As Variant


There is a problem with your algorithm. For one thing, it gives an incorrect
weeknumber for 2 Jan 2101.

Note the following:

Saturday, January 01, 2101 52
Sunday, January 02, 2101 53

Both are in week 52.

To calculate the ISO weeknumber, I suggest this, provided some years ago by
Daniel Maher:

=========================
Function ISOWeeknum(d1 As Date) As Integer
' Daniel Maher
Dim d2 As Long
d2 = DateSerial(Year(d1 - Weekday(d1 - 1) + 4), 1, 3)
ISOWeeknum = Int((d1 - d2 + Weekday(d2) + 5) / 7)
End Function
========================

You can then use this number in creating your various formats.
--ron