宮崎県延岡市 | スマイルカラーソフトウェア | ユーザースタイルに合わせたプログラミングでお客様に幸せをお届けします。

生年月日から現在の満年齢を算出

生年月日と指定日から、指定日現在での満年齢を算出します。
使いやすいように Function プロシージャにしました。日付が正しくない場合は 0 を返しますが、 “2017/12” 程度ならその年月の1日として処理できます。

Public Function GetAge(ByVal BirthDay As String, ByVal sDate As String) As Integer
    Dim xBirth As Date
    Dim xDate  As Date
    
    GetAge = 0
    If IsDate(BirthDay) Then
        xBirth = CDate(BirthDay)
        If IsDate(sDate) Then
            xDate = CDate(sDate)
            If Format(xDate, "mmdd") >= Format(xBirth, "mmdd") Then
                    GetAge = Year(xDate) - Year(xBirth)
                Else
                    GetAge = Year(xDate) - Year(xBirth) - 1
            End If
        End If
    End If
End Function

以下のように使用します。

    Dim sBirthDay As String
    sBirthDay = "2010/10/18"
    MsgBox Format(Date, "yyyy/mm/dd") & "現在の" & vbCrLf & _
           sBirthDay & "生まれの人の満年齢は、" & CStr(GetAge(sBirthDay, Format(Date, "yyyy/mm/dd"))) & "才です。"

結果は次のようになります。