G Guest Jan 26, 2006 #1 What's the fastest way to figure out with VB code the First Sunday of a month
W Wayne Morgan Jan 26, 2006 #2 Dim i As Integer, intFirstSunday As Integer For i = 1 To 7 If WeekDay(DateSerial(Year(Date), Month(Date), i)) = vbSunday Then intFirstSunday = i Exit For End If Next i Replace "Date" with whatever date value you are using.
Dim i As Integer, intFirstSunday As Integer For i = 1 To 7 If WeekDay(DateSerial(Year(Date), Month(Date), i)) = vbSunday Then intFirstSunday = i Exit For End If Next i Replace "Date" with whatever date value you are using.
J John Spencer Jan 26, 2006 #3 Don't know if this is the quickest, but here is one way. ?DateAdd("d",1- Weekday(DateSerial(Year(Date()),Month(Date()),7),1),DateSerial(Year(Date()),Month(Date()),7))
Don't know if this is the quickest, but here is one way. ?DateAdd("d",1- Weekday(DateSerial(Year(Date()),Month(Date()),7),1),DateSerial(Year(Date()),Month(Date()),7))
G Guest Jan 26, 2006 #4 That gives you the day of the week, but not the date. I added a line to do that Dim i As Integer, intFirstSunday As Integer Dim dtmFirstSunday as Date For i = 1 To 7 If WeekDay(DateSerial(Year(Date), Month(Date), i)) = vbSunday Then intFirstSunday = i Exit For End If Next i dtmFirstSunday = DateSerial(Year(Date), Month(Date), i)
That gives you the day of the week, but not the date. I added a line to do that Dim i As Integer, intFirstSunday As Integer Dim dtmFirstSunday as Date For i = 1 To 7 If WeekDay(DateSerial(Year(Date), Month(Date), i)) = vbSunday Then intFirstSunday = i Exit For End If Next i dtmFirstSunday = DateSerial(Year(Date), Month(Date), i)
J John Griffiths Jan 27, 2006 #5 ' Annotated version, split to allow step through Public Function FirstSunday(InputDate As Date) As Date Dim Result As Date ' Avoid problems if run midnight on last day of month Result = Date ' Change from current day of month to first day of month Result = DateSerial( Year(Result), Month(Result), 1 ) '----------------------------- 'Day - Weekday() Value - Needed Offset 'Sun - 0 - 0 'Mon - 1 - 6 'Tue - 2 - 5 'Wed - 3 - 4 'Thu - 4 - 3 'Fri - 5 - 2 'Sat - 6 - 1 Dim Offset As Integer Offset = (7 - Weekday( Result )) ' Deal with case of Sunday Offset = (Offset Mod 7 ) '----------------------------- Result = Result + Offset FirstSunday = Result End Function Finding the nth Particular Weekday in a Month http://msdn.microsoft.com/library/d...v/html/findingnthparticularweekdayinmonth.asp
' Annotated version, split to allow step through Public Function FirstSunday(InputDate As Date) As Date Dim Result As Date ' Avoid problems if run midnight on last day of month Result = Date ' Change from current day of month to first day of month Result = DateSerial( Year(Result), Month(Result), 1 ) '----------------------------- 'Day - Weekday() Value - Needed Offset 'Sun - 0 - 0 'Mon - 1 - 6 'Tue - 2 - 5 'Wed - 3 - 4 'Thu - 4 - 3 'Fri - 5 - 2 'Sat - 6 - 1 Dim Offset As Integer Offset = (7 - Weekday( Result )) ' Deal with case of Sunday Offset = (Offset Mod 7 ) '----------------------------- Result = Result + Offset FirstSunday = Result End Function Finding the nth Particular Weekday in a Month http://msdn.microsoft.com/library/d...v/html/findingnthparticularweekdayinmonth.asp