什么是VBA中IsLeapYear函数的良好实现?
编辑:我运行if-then和DateSerial实现,迭代包含在计时器中,并且DateSerial平均更快1-2毫秒(5次运行300次迭代,1个平均单元工作表公式也工作).
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
我最初从Chip Pearson的Excel网站上获得了这个功能.
皮尔逊的网站
public function isLeapYear (yr as integer) as boolean isLeapYear = false if (mod(yr,400)) = 0 then isLeapYear = true elseif (mod(yr,100)) = 0 then isLeapYear = false elseif (mod(yr,4)) = 0 then isLeapYear = true end function
维基百科更多... http://en.wikipedia.org/wiki/Leap_year
如果效率是一个考虑因素而且预期年份是随机的,那么首先做最常见的案例可能会稍好一些:
public function isLeapYear (yr as integer) as boolean if (mod(yr,4)) <> 0 then isLeapYear = false elseif (mod(yr,400)) = 0 then isLeapYear = true elseif (mod(yr,100)) = 0 then isLeapYear = false else isLeapYear = true end function