VBA code for doing date calculations PDF Print E-mail
Friday, 28 October 2005
some nice pieces of VBA code for doing date calculations can be very usefull for all the excel/vba coders out there

I used this function for checking if a given date is holiday (based on austrian holidays) ...you need the easter method (which you find below, to make that code work...


'checks if a given day is a holiday
  Private Function isHoliday(myDate As Date) As Boolean
     Select Case myDate
     'new year
     Case DateSerial(year(myDate), 1, 1)
     isHoliday = True
     '6.january - Epiphany (Heilige drei Koenige)
     Case DateSerial(year(myDate), 1, 6)
     isHoliday = True
     '1. may
     Case DateSerial(year(myDate), 5, 1)
     isHoliday = True
     'easter sunday
     Case Easter(year(myDate))
     isHoliday = True
     'easter monday
     Case Easter(year(myDate)) + 1
     isHoliday = True
     'Ascension Day (Christi Himmelfahrt)
     Case Easter(year(myDate)) + 39
     isHoliday = True
     'Whitsunday (pfingssonntag)
     Case Easter(year(myDate)) + 49
     isHoliday = True
     'Whitmonday (Pfingsmontag)
     Case Easter(year(myDate)) + 50
     isHoliday = True
     'Corpus Christi (Fronleichnam)
     Case Easter(year(myDate)) + 60
     isHoliday = True
     '15. august - Assumption Day (Maria Himmelfahrt)
     Case DateSerial(year(myDate), 8, 15)
     isHoliday = True
     '26. oct. national holiday (austria)
     Case DateSerial(year(myDate), 10, 26)
     isHoliday = True
     '1. nov All Saints' Day (Allerheiligen)
     Case DateSerial(year(myDate), 11, 1)
     isHoliday = True
     '8. dec immaculate conception (Maria Empfaengnis)
     Case DateSerial(year(myDate), 12, 8)
     isHoliday = True
     '25. dec christmas
     Case DateSerial(year(myDate), 12, 25)
     isHoliday = True
     '26. dec Stefanitag
     Case DateSerial(year(myDate), 12, 26)
     isHoliday = True
     End Select       
End Function

nice function for calculating the easter day, if you only know the year (did not write that by myself, found it somewhere on the web...)

'calculates the date of the easter day of the given year
  Function Easter(year As Integer) As Date
     Dim d As Integer
     d = (((255 - 11 * (year Mod 19)) - 21) Mod 30) + 21
     Easter = DateSerial(year, 3, 1) + d + (d > 48) + 6 - _
     ((year + year \ 4 + d + (d > 48) + 1) Mod 7)
  End Function

another one to find out if a given year is a leap year or not

'calculates if the given year is a leap year or not
  Function leapYear(year As Integer) As Boolean
     If (year Mod 4) = 0 And (year Mod 100) <> 0 Or _
        ((year Mod 400) = 0) Then
        leapYear = True
     Else
        leapYear = False
     End If
  End Function

'returns the lenght of the given month (january = 1)
  Function monthLenght(year As Integer, month As Integer) As Integer
     Select Case month
     Case 2
     If leapYear(year) Then
     monthLenght = 29
     Else
     monthLenght = 28
     End If
     Case 4, 6, 9, 11
     monthLenght = 30
     Case Else
     monthLenght = 31
     End Select
 End Function
Last Updated ( Wednesday, 21 December 2005 )
 
< Prev   Next >
© 1999 - 2009 Gerald Haider, Vienna . All trademarks and copyrights on this page are owned by their respective owners.