英語で読む

次の方法で共有


Excel で 1900 年 1 月 1 日より前の年齢を計算する方法

概要

Microsoft Excel の日付式では、1900 年 1 月 1 日から 9999 年 12 月 31 日の間に入力された日付のみを使用できますが、カスタムの Microsoft Visual Basic for Applications 関数を使用して、1900 年 1 月 1 日より前に最初に作成されたユーザーの年齢 (年単位) を計算できます。

マクロを使用して年齢を計算する

Microsoft は、例示のみを目的としてプログラミング例を提供しており、明示または黙示にかかわらず、いかなる責任も負わないものとします。 これには、市販性または特定の目的との適合性についての黙示の保証も含まれますが、これに限定はされません。 この記事は、説明されているプログラミング言語、手順を作成およびデバッグするために使用されているツールに読者が精通していることを前提にしています。 マイクロソフト サポート窓口では、特定のプロシージャの機能説明に関するご質問に対して支援いたしますが、本例を特定の目的を満たすために機能を追加したり、プロシージャを構築することは行いません。

Excel では、1900 年 1 月 1 日より前の日付をテキストとして入力します。 この関数は、1/1/0001、通常の日付で始まるテキストとして入力された日付に対して機能し、開始日が 1900 より前で終了日が 1900 より後の日付を処理できます。 マクロを使用するには、次の手順に従います。

  1. Excel を起動します。 関数を使用するワークシートを表示します。

  2. Alt キーを押しながら F11 キーを押して、Visual Basic エディターに切り替えます。

  3. [挿入] メニューの [モジュール] をクリックします。

  4. モジュールに次のコードを入力します。

    VB
    ' This is the initial function. It takes in a start date and an end date.
    Public Function AgeFunc(stdate As Variant, endate As Variant)
    
        ' Dim our variables.
        Dim stvar As String
        Dim stmon As String
        Dim stday As String
        Dim styr As String
        Dim endvar As String
        Dim endmon As String
        Dim endday As String
        Dim endyr As String
        Dim stmonf As Integer
        Dim stdayf As Integer
        Dim styrf As Integer
        Dim endmonf As Integer
        Dim enddayf As Integer
        Dim endyrf As Integer
        Dim years As Integer
    
        ' This variable will be used to modify string length.
        Dim fx As Integer
        fx = 0
    
        ' Calls custom function sfunc which runs the Search worksheet function
        ' and returns the results.
        ' Searches for the first "/" sign in the start date.
        stvar = sfunc("/", stdate)
    
        ' Parse the month and day from the start date.
        stmon = Left(stdate, sfunc("/", stdate) - 1)
        stday = Mid(stdate, stvar + 1, sfunc("/", stdate, sfunc("/", stdate) + 1) - stvar - 1)
    
        ' Check the length of the day and month strings and modify the string 
        ' length variable.
        If Len(stday) = 1 Then fx = fx + 1
        If Len(stmon) = 2 Then fx = fx + 1
    
        ' Parse the year, using information from the string length variable.
        styr = Right(stdate, Len(stdate) - (sfunc("/", stdate) + 1) - stvar + fx)
    
        ' Change the text values we obtained to integers for calculation 
        ' purposes.
        stmonf = CInt(stmon)
        stdayf = CInt(stday)
        styrf = CInt(styr)
    
        ' Check for valid date entries.
        If stmonf < 1 Or stmonf > 12 Or stdayf < 1 Or stdayf > 31 Or styrf < 1 Then
            AgeFunc = "Invalid Date"
            Exit Function
        End If
    
        ' Reset the string length variable.
        fx = 0
    
        ' Parse the first "/" sign from the end date.
        endvar = sfunc("/", endate)
    
       ' Parse the month and day from the end date.
        endmon = Left(endate, sfunc("/", endate) - 1)
        endday = Mid(endate, endvar + 1, sfunc("/", endate, sfunc("/", endate) + 1) - endvar - 1)
    
        ' Check the length of the day and month strings and modify the string 
        ' length variable.
        If Len(endday) = 1 Then fx = fx + 1
        If Len(endmon) = 2 Then fx = fx + 1
    
        ' Parse the year, using information from the string length variable.
        endyr = Right(endate, Len(endate) - (sfunc("/", endate) + 1) - endvar + fx)
    
        ' Change the text values we obtained to integers for calculation 
        ' purposes.
        endmonf = CInt(endmon)
        enddayf = CInt(endday)
        endyrf = CInt(endyr)
    
        ' Check for valid date entries.
        If endmonf < 1 Or endmonf > 12 Or enddayf < 1 Or enddayf > 31 Or endyrf < 1 Then
            AgeFunc = "Invalid Date"
            Exit Function
        End If
    
        ' Determine the initial number of years by subtracting the first and 
        ' second year.
        years = endyrf - styrf
    
        ' Look at the month and day values to make sure a full year has passed. 
        If stmonf > endmonf Then
            years = years - 1
        End If
    
    If stmonf = endmonf And stdayf > enddayf Then
            years = years - 1
        End If
    
        ' Make sure that we are not returning a negative number and, if not, 
        ' return the years.
        If years < 0 Then
            AgeFunc = "Invalid Date"
        Else
            AgeFunc = years
        End If
    
    End Function
    
    ' This is a second function that the first will call.
    ' It runs the Search worksheet function with arguments passed from AgeFunc.
    ' It is used so that the code is easier to read.
    Public Function sfunc(x As Variant, y As Variant, Optional z As Variant)
        sfunc = Application.WorksheetFunction.Search(x, y, z)
    End Function
    
  5. ファイルを保存します。

  6. 次のデータを入力します。

    asciidoc
     A1 01/01/1887
     A2 02/02/1945
    

    セル A3 に、次の数式を入力します。

    excel
    =AgeFunc(startdate,enddate)
    

    開始日は最初の日付 (A1) へのセル参照であり、enddate は 2 番目の日付 (A2) へのセル参照です。

    結果は 58 である必要があります。

注意

有効期間については、1900 年 1 月 1 日より前のすべての日付を確認してください。 テキストとして入力された日付は、Excel ではチェックされません。

関連情報

この記事のサンプル コードの使用方法の詳細については、「 Office 2010 のサポート技術情報記事からサンプル コードを実行する方法」を参照してください。