Leap Year Test - VBA

Ziggy12

Active Member
Joined
Jun 26, 2002
Messages
365
Want to test for a leap year in VBA. How do I check if the result of dividing the year by 4 is an whole number?

:confused:
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Actually it's pretty easy...just use the MOD operator to determine if there is a remainder from the division.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestLeapYear()
    <SPAN style="color:#00007F">If</SPAN> (Year(Date) Mod 4) <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Not a Leap Year!"
    <SPAN style="color:#00007F">Else</SPAN>
        MsgBox "Leap Year!"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Hi Tommy and Ziggy:

The century years, of course must be divisible by 400 to qualify as a Leap Year -- with the exception of 1900, which is incorrectly assumed in EXCEL to be a Leap Year.
 
Upvote 0
That's only years ending in 00 e.g. 2000. for all other years if the result of dividing by 4 is an integer then it's a leap year.

cheers ziggy12
:p
 
Upvote 0
With minimal testing:

Code:
Function isLeapYear(ByVal YY As Long) As Boolean
    isLeapYear = IIf(YY Mod 100 = 0, YY Mod 400 = 0, YY Mod 4 = 0)
    End Function
This is usable either in VBA or as an XL function.

Ziggy12 said:
Want to test for a leap year in VBA. How do I check if the result of dividing the year by 4 is an whole number?

:confused:
 
Upvote 0
Public Function IsLeapYear(Y As Integer)
IsLeapYear = Month(DateSerial(Y, 2, 29)) = 2
End Function

Works too but... Both code replies with true or not true when the input is the year only (no dd and mm). In my case the input is day, month ánd year what results in wrong response.

Example: Input of cell A1 is 12-12-2004. However formatted to "yyyy" Cell A2 (formula =IsleapYear) shows "not true" and not "true".

Is there a way to get this fixed?
 
Upvote 0
Wil Moosa said:
Public Function IsLeapYear(Y As Integer)
IsLeapYear = Month(DateSerial(Y, 2, 29)) = 2
End Function

Works too but... Both code replies with true or not true when the input is the year only (no dd and mm). In my case the input is day, month ánd year what results in wrong response.

Example: Input of cell A1 is 12-12-2004. However formatted to "yyyy" Cell A2 (formula =IsleapYear) shows "not true" and not "true".

Is there a way to get this fixed?
Hi Will:

If I have understood you correctly, Tushar's function as posted in this thread can be modified to use date rather than year as in the following ...
Code:
Function isyLeapYear(ByVal Date1 As Date) As Boolean
    YY = Year(Date1)
    isyLeapYear = IIf(YY Mod 100 = 0, YY Mod 400 = 0, YY Mod 4 = 0)
End Function
Is this what you are looking for?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top