Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default WeekNum Trouble

I've wrote a function to take an integer representing the Calendar Week and
return the date of the monday of that week. A reverse of the weeknum
function. Works fine when I paste the formula in a worksheet, but I get a
compile error in vba. Here's the code.

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of
that week
'
Dim sSunday As Date

sSunday = Date(Year(Today()), 1, 1) + _
(7 - Weekday(Date(Year(Today()), 1, 1)) - 6) + (iCalWeek * 7)

GetDate = sSunday + 1

End Function

any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default WeekNum Trouble

Use DateSerial instead of date().
Use Date instead of Today()

Both =date() and =today() are worksheet functions.



Brad wrote:

I've wrote a function to take an integer representing the Calendar Week and
return the date of the monday of that week. A reverse of the weeknum
function. Works fine when I paste the formula in a worksheet, but I get a
compile error in vba. Here's the code.

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of
that week
'
Dim sSunday As Date

sSunday = Date(Year(Today()), 1, 1) + _
(7 - Weekday(Date(Year(Today()), 1, 1)) - 6) + (iCalWeek * 7)

GetDate = sSunday + 1

End Function

any ideas?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default WeekNum Trouble

I think this function does what you want...

Function GetDate(iCalWeek As Integer) As Date
GetDate = DateSerial(Year(Now), 1, 7 * (iCalWeek - 1) + 1)
GetDate = GetDate - Weekday(GetDate) + 2
End Function

--
Rick (MVP - Excel)


"Brad" wrote in message
...
I've wrote a function to take an integer representing the Calendar Week
and
return the date of the monday of that week. A reverse of the weeknum
function. Works fine when I paste the formula in a worksheet, but I get a
compile error in vba. Here's the code.

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of
that week
'
Dim sSunday As Date

sSunday = Date(Year(Today()), 1, 1) + _
(7 - Weekday(Date(Year(Today()), 1, 1)) - 6) + (iCalWeek * 7)

GetDate = sSunday + 1

End Function

any ideas?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default WeekNum Trouble

Thanks!

"Dave Peterson" wrote:

Use DateSerial instead of date().
Use Date instead of Today()

Both =date() and =today() are worksheet functions.



Brad wrote:

I've wrote a function to take an integer representing the Calendar Week and
return the date of the monday of that week. A reverse of the weeknum
function. Works fine when I paste the formula in a worksheet, but I get a
compile error in vba. Here's the code.

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of
that week
'
Dim sSunday As Date

sSunday = Date(Year(Today()), 1, 1) + _
(7 - Weekday(Date(Year(Today()), 1, 1)) - 6) + (iCalWeek * 7)

GetDate = sSunday + 1

End Function

any ideas?


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
VP VP is offline
external usenet poster
 
Posts: 1
Default WeekNum Trouble

Here is my take on this -

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of

'
If (iCalWeek 0 And iCalWeek <= 52) Then
Else
MsgBox ("Please enter a valid week")
Exit Function
End If

MyDate = DateSerial(Year(Date), 1, 1)
' Assign a date.
MyWeekDay = Weekday(MyDate) ' Get the day for First of the year

If (MyWeekDay 2 And iCalWeek = 1) Then
MsgBox ("There was no Monday in the first week")
Exit Function
End If
'TODO: Add another validation for the number 52, if there is no monday
in the 52nd week etc

Dim tempDate As Date
If (MyWeekDay = 2) Then
tempDate = DateAdd("ww", iCalWeek - 1, MyDate)
Else
tempDate = DateAdd("ww", iCalWeek, MyDate)
End If


Dim sMonday As Date
' To get the Monday for the current week, use this
'sMonday = DateAdd("d", -Weekday(Date) + 2, Date)

sMonday = DateAdd("d", -Weekday(tempDate) + 2, tempDate)
GetDate = sMonday
End Function


Cheers,
Varun
(Sydney)
"Brad" wrote:

I've wrote a function to take an integer representing the Calendar Week and
return the date of the monday of that week. A reverse of the weeknum
function. Works fine when I paste the formula in a worksheet, but I get a
compile error in vba. Here's the code.

Function GetDate(iCalWeek As Integer) As Date
'
' Takes calendar week num as an argument and returns the date of monday of
that week
'
Dim sSunday As Date

sSunday = Date(Year(Today()), 1, 1) + _
(7 - Weekday(Date(Year(Today()), 1, 1)) - 6) + (iCalWeek * 7)

GetDate = sSunday + 1

End Function

any ideas?

Reply
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
weeknum reno Excel Discussion (Misc queries) 2 May 15th 08 11:56 PM
=WEEKNUM TheRook Excel Programming 1 August 25th 06 08:53 AM
Weeknum help SoSoExcelGuy Excel Worksheet Functions 2 August 1st 06 03:14 PM
WEEKNUM() Ciara Excel Discussion (Misc queries) 5 April 13th 05 12:09 PM
WeekNum Trouble WillRn Excel Programming 3 November 9th 04 04:14 PM


All times are GMT +1. The time now is 07:43 AM.

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

About Us

"It's about Microsoft Excel"