Monday, July 03, 2006

VBA Leap Year Test

Working on a calendar project I needed a test for leap years. As an alternative to the standard 'divisible by four' approach, try the following which even works for 1900:
Function QueryLeapYear()

Dim d As Integer

On Error GoTo handler
d = Weekday("29/02/1900")
Exit Function
handler:
Debug.Print "Not a leap  year."

End Function

Control passes to the error handler if the Weekday() function cannot return a day number from the supplied date.