質問

何が良い実装のIsLeapYear機能VBA?

編集: った場合-そのDateSerial実装を繰り返しに包まれたタイマー、DateSerialたより迅速に平均1-2ms(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  

私はもともとってこの機能からチップのピアソン大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

Wikipediaのために---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

バリエーションとしてのチップのピアソンの溶液に、このまま試み

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

こんに CodeToad :

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

がんのIsDateる機能は以下のカップルの場合、elseifs.

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
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

私は多くのコンセプトなのかを示す追加の理解 使用日の機能を素晴ら...のコード効率..を考慮するマシンコードに必要な機能を実行す

より複雑な機能日 利用のみにかなりの高速整数の機能 基礎が築いた後藤 思うように以下の高速化

  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

遅い応答の性質問です。

TL/DR:の 数学 バージョンは 5x速


いつの回答をこちら

  1. 数学的解釈のうるう年の定義
  2. 利用Excelの日付/時刻を検知する機能月29日(これらは二つのキャンプ:の日付を文字列として、せん)

を行った時に試験掲載していますので、答えを、発見したの 数学 方法につ 5x速 以外の日時ます。


それかの最適化の方法もいただきました(気にしないで見てください 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倍の

結果

  • 数バージョン:640ms
  • 日付/時刻バージョン: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

こちらはもう一つの簡単なオプションです。

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

まLeap_Day_Check=28ではうるう年の場合は29日です。

VBA何日前の1月には年間で設定では28 29年月ます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top