Thread: Weeknum
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 Weeknum

On Fri, 16 Sep 2005 22:15:27 -0400, Ron Rosenfeld
wrote:

On Fri, 16 Sep 2005 16:14:42 -0500, Baapi
wrote:


How to mimic the "Weeknum" function of Excel in the Script? :)


This comes close:

===============
Function VBWeeknum(dt As Date, _
Optional FirstDayOfWeek As Integer = 1) As Integer

VBWeeknum = DatePart("ww", dt, FirstDayOfWeek, vbFirstJan1)
End Function
================

One difference is that the worksheet function WEEKNUM only accepts Sunday or
Monday as being the first day of the week, whereas this UDF can accept any day
as being day 1.

So if you wanted to truly mimic the Weeknum function, you might check that
FirstDayOfWeek is 1 or 2, and if not, return a #NUM! error.


--ron


Thanks to Norman Jones, we can now do a better job of mimicking the WEEKNUM
function, including errors for invalid entries:

===================================
Function VBWeeknum(dt As Date, _
Optional FirstDayOfWeek As Integer = 1)

Select Case FirstDayOfWeek
Case 1 To 2
VBWeeknum = DatePart("ww", dt, FirstDayOfWeek, vbFirstJan1)
Case Else
VBWeeknum = CVErr(xlErrNum)
End Select
End Function
==================================


--ron