Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Tell me which "season" (Month/Day through Month/Day) a date(Month/Day/Year) falls in (any year)??? | Excel Discussion (Misc queries) | |||
Which historical year will offer the same weekday ... ? | Excel Discussion (Misc queries) | |||
find date of last weekday of a month | Excel Discussion (Misc queries) | |||
Weekday formula calculating to end of month | Excel Worksheet Functions | |||
WEEKDAY question on year | Excel Discussion (Misc queries) |