LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Xth Weekday of the Month/Year

I ran in to a need for a function that would give the date of the 3rd Sunday
in October and the First Thursday in April. This is a common problem when
trying to adjust time zone differences internationally. e.g. what time is it
in Cairo and Sydney when it is 10:00AM in London on 3 March 2005. What about
4 June 2006? etc.

You can do this through a macro or non-macro solution. The non-Macro would
have a formula that looks like this for getting the Last Weekday in the Month:
=EOMONTH(C14,0) - Mod(Mod(7-Mod(L14,7),7)+WeekD(EOMONTH(C14,0)),7)

The non-Macro would look like this for getting the First Weekday in the Month:
=DATE(YEAR(C14),MONTH(C14),1) +
Mod(7-Mod(Mod(7-Mod(L14,7),7)+WeekD(DATE(YEAR(C14),MONTH(C14),1)), 7),7) +
(J14-1)*7

Where C14 is the date that contains the target Month and Year. L14 contains
the weekday (using microsoft weekdays: 1 is Sat, 2 is Sun, ... 7 is Fri).
J14 is the number of occurrences; 1 would be the 1st weekday (e.g. Tuesday)
of the month, 2 would be the 2nd etc.

If you wanted to know the Xth weekday of the year (e.g. what is the 24th
Wednesday of 2006), then set the month to January. (e.g. C14 would equal
1/1/2006, L14 would equal 5 for Wed, and J14 would equal 24).

Here is the macro version. You can implement this as a function:
XthWeekDay(Occurrence, WeekD, Mo, Yr)
e.g. the Last Tuesday in November 2005 would be:
=XthWeekDay("Last", 4, 11, 2005)
the 1st Tuesday in November 2008 would be:
=XthWeekDay(1, 4, 11, 2005)
the 32nd Saturday in 2011 would be:
=XthWeekDay(32, 1, 1, 2011)

Public Function XthWeekDay(Occurrence As Variant, WeekD As Integer, Mo As
Integer, Yr As Integer)
'Return the Xth Weekday of a given month and year. Use "Last" for last and
a number for 1st, 2nd, etc.
'Mark Fitzpatrick

Dim OffSetDay As Date

'Set Offset Day
be = 0
If Occurrence = "Last" Then be = 1

If Mo = 12 Then
OffSetDay = DateSerial(Yr, 12, 30 * be + 1)
Else
OffSetDay = DateSerial(Yr, Mo + be, 1) - be
End If


'Set the Xth Day
If Occurrence = "Last" Then
XthWeekDay = OffSetDay - Modulo(Modulo(7 - Modulo(WeekD, 7), 7) +
Weekday(OffSetDay), 7)
Else
XthWeekDay = OffSetDay + Modulo(7 - Modulo(Modulo(7 - Modulo(WeekD, 7),
7) + Weekday(OffSetDay), 7), 7) + 7 * (Occurrence - 1)
End If

End Function


Private Function Modulo(x As Integer, y As Integer) As Integer

Modulo = Abs(x / y - Int(x / y)) * y

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
Tell me which "season" (Month/Day through Month/Day) a date(Month/Day/Year) falls in (any year)??? misscrf Excel Discussion (Misc queries) 1 December 14th 07 02:59 PM
Which historical year will offer the same weekday ... ? Eric Excel Discussion (Misc queries) 5 March 16th 07 03:13 PM
find date of last weekday of a month TUNGANA KURMA RAJU Excel Discussion (Misc queries) 9 October 28th 06 02:51 PM
Weekday formula calculating to end of month DebbieK Excel Worksheet Functions 0 July 26th 06 08:08 PM
WEEKDAY question on year mrjeep Excel Discussion (Misc queries) 2 May 3rd 06 09:02 AM


All times are GMT +1. The time now is 10:04 PM.

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"