ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Perform a task if result is a whole number (https://www.excelbanter.com/excel-programming/380385-perform-task-if-result-whole-number.html)

[email protected]

Perform a task if result is a whole number
 
I am trying to include some logic that will determine leap years. I
want to perform a given loop if the year is evenly divisible by 4. So,
if year/4 = a whole number, then do the loop.

What is the easiest way to accomplish this?


Jason Lepack

Perform a task if result is a whole number
 
Public Function isLeapYear(yr As Integer) As Boolean
isLeapYear = False
If yr Mod 4 = 0 Then isLeapYear = True
End Function

wrote:
I am trying to include some logic that will determine leap years. I
want to perform a given loop if the year is evenly divisible by 4. So,
if year/4 = a whole number, then do the loop.

What is the easiest way to accomplish this?



Dave Peterson

Perform a task if result is a whole number
 
1900 is divisible by 4, but it's not a leap year. Same with 2100, 2200, 2300.
If the year is a century, it has to be divisible by 400.

You could rely on the way excel/VBA treats dates:

Function IsLeapYear(myYear As Long) As Boolean
if month(dateserial(myYear,2,29)) = 2 then
isLeapYear = true
else
IsLeapYear = false
end if
End Function

Excel and VBA are pretty smart when it comes to dates.

wrote:

I am trying to include some logic that will determine leap years. I
want to perform a given loop if the year is evenly divisible by 4. So,
if year/4 = a whole number, then do the loop.

What is the easiest way to accomplish this?


--

Dave Peterson


All times are GMT +1. The time now is 11:09 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com