LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Re-Post: Help w/ WeekNum UDF

Please excuse my re-post .....

I'm using a UDF (found in this newsgroup!) to calculate the week
number from a user-entered date ... the code for this UDF is below.
All works ok except when the date entered is a Sunday -- this is the
first day of the week, but the UDF calcs it as part of the previous
week.

I tried both 'WeekStart' options, neither one gives the right answer.
As example, June 21 should come back as week 26. I'm using Excel 07
(Compatability mode) on Windows XP.

Any ideas on what needs to be changed (if anything)?

Thanks,
Ray

Function WKNUM(TheDate As Date, WeekStart As Integer)

' Using the 1900 date system.
' WeekStart = 1 Week starts on Sunday
' WeekStart = 2 Week starts on Monday

Dim TempDate As Date
Dim ExtraDays As Integer

If WeekStart < 1 And WeekStart < 2 Then
WKNUM = "#NUM!"
Exit Function
End If

TempDate = TheDate + 4 - Weekday(TheDate + 1 - WeekStart)
ExtraDays = (Weekday(DateSerial(Year(TempDate), 1, 2 - WeekStart))
+ 2) Mod 7
WKNUM = Int((TempDate - DateSerial(Year(TempDate), 1, 1) +
ExtraDays) / 7) + 1

End Function
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sub to get post code to post code mileage from web site Atishoo Excel Programming 3 June 29th 09 02:12 PM
Weeknum weeknum hell Excel Worksheet Functions 1 May 6th 09 03:50 PM
Should I generally request "post a poll" when I post a new thread? Joe Miller Excel Discussion (Misc queries) 2 January 7th 06 04:46 PM
Weeknum Baapi[_3_] Excel Programming 5 September 18th 05 07:18 PM
WEEKNUM() Ciara Excel Discussion (Misc queries) 5 April 13th 05 12:09 PM


All times are GMT +1. The time now is 05:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"