如何找到Leapyear在VBA?(How do you find Leapyear in VBA?

2019-08-01 07:16发布

什么是一个很好的实施VBA一个IsLeapYear功能?

编辑:我跑的IF-THEN和DateSerial执行与包裹在一个定时器的迭代,并且DateSerial的速度更快的平均通过1-2毫秒(300次迭代5次,用1点平均细胞工作表式也工作)。

Answer 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  

我本来得到了芯片皮尔逊大Excel的网站这个功能。

皮尔逊的网站



Answer 2:

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



Answer 3:

如果效率是一个考虑因素,并预期今年是随机的,那么它可能会稍微好一点,首先要做的最常见的情况:

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


Answer 4:

由于在芯片解决方案皮尔森的变化,你也可以尝试

Public Function isLeapYear(Yr As Integer) As Boolean  

  ' returns FALSE if not Leap Year, TRUE if Leap Year  

  isLeapYear = (DAY(DateSerial(Yr, 3, 0)) = 29)  

End Function


Answer 5:

我发现这个有趣的一个CodeToad :

Public Function IsLeapYear(Year As Varient) As Boolean
  IsLeapYear = IsDate("29-Feb-" & Year)
End Function 

虽然我敢肯定,在功能使用则IsDate的可能比一对夫妇如果elseifs慢。



Answer 6:

Public Function ISLeapYear(Y As Integer) AS Boolean
 ' Uses a 2 or 4 digit year
'To determine whether a year is a leap year, follow these steps:
'1    If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
'2    If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
'3    If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
'4    The year is a leap year (it has 366 days).
'5    The year is not a leap year (it has 365 days).

If Y Mod 4 = 0 Then ' This is Step 1 either goto step 2 else step 5
    If Y Mod 100 = 0 Then ' This is Step 2 either goto step 3 else step 4
        If Y Mod 400 = 0 Then ' This is Step 3 either goto step 4 else step 5
            ISLeapYear = True ' This is Step 4 from step 3
                Exit Function
        Else: ISLeapYear = False ' This is Step 5 from step 3
                Exit Function
        End If
    Else: ISLeapYear = True ' This is Step 4 from Step 2
            Exit Function
    End If
Else: ISLeapYear = False ' This is Step 5 from Step 1
End If


End Function


Answer 7:

Public Function isLeapYear(Optional intYear As Variant) As Boolean

    If IsMissing(intYear) Then
        intYear = Year(Date)
    End If

    If intYear Mod 400 = 0 Then
        isLeapYear = True
    ElseIf intYear Mod 4 = 0 And intYear Mod 100 <> 0 Then
        isLeapYear = True
    End If

End Function


Answer 8:

我看到指出额外理解和日期函数是了不起从...到学习的代码效率方面使用许多伟大的概念..考虑需要执行功能的机器代码

而不是复杂的日期函数只使用相当快的整数功能的基本是建立在GOTO我怀疑,类似下面是快

  Function IsYLeapYear(Y%) As Boolean
     If Y Mod 4 <> 0 Then GoTo NoLY ' get rid of 75% of them
     If Y Mod 400 <> 0 And Y Mod 100 = 0 Then GoTo NoLY
     IsYLeapYear = True

NoLY:

 End Function


Answer 9:

迟到的回答解决问题的表现。

TL / DR: 数学版本是约5倍快


我看到的答案两组这里

  1. 闰年定义的数学解释
  2. 利用Excel的日期/时间功能检测年02月29(这些分为两个阵营:那些建造日期为字符串,而那些不)

我跑在所有公布答案的时间测试中,发现了一个数学方法约5倍比日期/时间的方法更快


然后我做的方法的一些优化,并想出了(相信与否Integer比稍快Long在这种情况下,不知道为什么。)

Function IsLeapYear1(Y As Integer) As Boolean
    If Y Mod 4 Then Exit Function
    If Y Mod 100 Then
    ElseIf Y Mod 400 Then Exit Function
    End If
    IsLeapYear1 = True
End Function

为了便于比较,我想出了(相差无几的发布版本)

Public Function IsLeapYear2(yr As Integer) As Boolean
    IsLeapYear2 = Month(DateSerial(yr, 2, 29)) = 2
End Function

即建立一个日期作为字符串的日期/时间版本被打折扣,因为它们又慢得多。

该测试是让IsLeapYear多年100..9999,重复1000次

结果

  • 数学版:640毫秒
  • 日期/时间版本:3360ms

测试代码是

Sub Test()
    Dim n As Long, i As Integer, j As Long
    Dim d As Long
    Dim t1 As Single, t2 As Single
    Dim b As Boolean

    n = 1000

    Debug.Print "============================="
    t1 = Timer()
    For j = 1 To n
    For i = 100 To 9999
        b = IsYLeapYear1(i)
    Next i, j
    t2 = Timer()
    Debug.Print 1, (t2 - t1) * 1000

    t1 = Timer()
    For j = 1 To n
    For i = 100 To 9999
        b = IsLeapYear2(i)
    Next i, j
    t2 = Timer()
    Debug.Print 2, (t2 - t1) * 1000
End Sub


Answer 10:

这里还有一个简单的选择。

Leap_Day_Check = Day(DateValue("01/03/" & Required_Year) - 1)

如果Leap_Day_Check = 28那么它是不是闰年,如果是29则。

VBA知道3月1日之前的日期是一年,因此将其设置为任何28或2月29日我们。



文章来源: How do you find Leapyear in VBA?