영어로 읽기

다음을 통해 공유


Excel에서 1900년 1월 1일 이전의 나이를 계산하는 방법

요약

Microsoft Excel 날짜 수식은 1900년 1월 1일부터 9999년 12월 31일 사이에 입력한 날짜만 사용할 수 있지만 사용자 지정 Microsoft Visual Basic for Applications 함수를 사용하여 1900년 1월 1일 이전에 처음 만든 사람 또는 항목의 나이(년)를 계산할 수 있습니다.

매크로를 사용하여 나이 계산

Microsoft에서 제공하는 프로그래밍 예제는 예시를 위한 것일 뿐이며 이와 관련하여 명시적이거나 묵시적인 어떠한 보증도 하지 않습니다. 이는 상품성이나 특정 목적에 대한 적합성의 묵시적인 보증을 포함하며 이에 제한되지 않습니다. 이 문서에서는 예제에 사용되고 있는 프로그래밍 언어와 프로시저를 만들고 디버깅하는 데 사용되는 도구를 사용자가 잘 알고 있는 것으로 가정합니다. Microsoft 지원 엔지니어는 사용자에게 도움이 되도록 특정 프로시저에 대한 기능을 설명할 수 있지만 사용자의 특정 요구 사항에 맞도록 예제를 수정하여 추가 기능을 제공하거나 프로시저를 구성하지는 않습니다.

Excel은 1900년 1월 1일 이전 날짜를 텍스트로 입력합니다. 이 함수는 일반 날짜인 1/1/0001부터 텍스트로 입력된 날짜에 대해 작동하며 시작 날짜가 1900 이전이고 종료 날짜가 1900년 이후인 날짜를 처리할 수 있습니다. 매크로를 사용하려면 다음 단계를 수행합니다.

  1. Excel을 시작합니다. 함수를 사용할 워크시트를 봅니다.

  2. Alt+F11을 눌러 Visual Basic Editor로 전환합니다.

  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)
    

    startdate는 첫 번째 날짜(A1)에 대한 셀 참조이고 enddate는 두 번째 날짜(A2)에 대한 셀 참조입니다.

    결과는 58이어야 합니다.

참고

유효성은 1900년 1월 1일 이전의 모든 날짜를 확인합니다. 텍스트로 입력한 날짜는 Excel에서 확인할 수 없습니다.

참조

이 문서의 샘플 코드를 사용하는 방법에 대한 자세한 내용은 Office 2010의 기술 자료 문서에서 샘플 코드를 실행하는 방법을 참조하세요.