What is a good implementation of a IsLeapYear function in VBA?
Edit: I ran the if-then and the DateSerial implementation with iterations wrapped in a timer, and the DateSerial was quicker on the average by 1-2 ms (5 runs of 300 iterations, with 1 average cell worksheet formula also working).
=MONTH(DATE(YEAR(A2),2,29))=2 Year is a Leap Year in Excel. After Excel has returned the initial result, copy the formula into the cells down the column for the next results to be returned.
Step 1: Create a sub procedure by naming the macro. Step 2: Declare the variable as “Date.” The DATE function returns the result as a date only, so the variable data type should be “Date.” Step 3: Assign the value to variable “k” as the DATE function.
IsDate is the VBA function that tests whether the given value is the date or not. If the supplied value or range reference value is the date value, we will get the result as “TRUE.” On the other hand, if the value is not date, we will get the result as “FALSE.” So, the result is a BOOLEAN value, i.e., TRUE or FALSE.
Example #1 – To Find Differences in DaysStep 1: Create a macro name first. Step 2: Define Two Variables as Date. Step 3: Now, for the Date1 variable, assign “15-01-2018,” and for the Date2 variable, assign “15-01-2019.” Step 4: Now, define one more variable, “As Long,” to store results.
Public Function isLeapYear(Yr As Integer) As Boolean
' returns FALSE if not Leap Year, TRUE if Leap Year
isLeapYear = (Month(DateSerial(Yr, 2, 29)) = 2)
End Function
I originally got this function from Chip Pearson's great Excel site.
Pearson's site
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With