Berechnen von Altern vor dem 1.1.1900 in Excel

Zusammenfassung

Obwohl Microsoft Excel-Datumsformeln nur Datumsangaben verwenden können, die zwischen dem 01.01.1900 und dem 31.12.9999 eingegeben wurden, können Sie eine benutzerdefinierte Microsoft Visual Basic for Applications-Funktion verwenden, um das Alter (in Jahren) einer Person oder etwas zu berechnen, das vor dem 1. Januar 1900 erstellt wurde.

Verwenden eines Makros zum Berechnen des Alters

Die Verwendung der hier aufgeführten Informationen, Makro- oder Programmcodes geschieht auf Ihre eigene Verantwortung. Microsoft stellt Ihnen diese Informationen sowie Makro- und Programmlistings ohne Gewähr auf Richtigkeit, Vollständigkeit und/oder Funktionsfähigkeit sowie ohne Anspruch auf Support zur Verfügung. Die zur Verfügung gestellten Makro- und Programmierungsbeispiele sollen lediglich exemplarisch die Funktionsweise des Beispiels aufzeigen. Die Microsoft Support-Spezialisten können bei der Erläuterung der Funktionalität bestimmter Prozeduren helfen, jedoch werden sie diese Beispiele nicht in Bezug auf eine erweiterte Funktionalität verändern, noch werden sie Prozeduren entwickeln, die auf Ihre besonderen Bedürfnisse zugeschnitten sind.

Excel gibt Datumsangaben vor dem 01.01.1900 als Text ein. Diese Funktion funktioniert für Datumsangaben, die als Text eingegeben werden, beginnend mit dem 1.1.0001, normale Datumsangaben, und kann Datumsangaben verarbeiten, wenn das Startdatum vor 1900 liegt und das Enddatum nach 1900 liegt. Führen Sie die folgenden Schritte aus, um das Makro zu verwenden:

  1. Starten Sie Excel. Zeigen Sie das Arbeitsblatt an, auf dem Sie die Funktion verwenden möchten.

  2. Drücken Sie ALT+F11, um zum Visual Basic-Editor zu wechseln.

  3. Klicken Sie im Menü „Einfügen“ auf „Modul“.

  4. Geben Sie den folgenden Code in das Modul ein:

    ' 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. Speichern Sie die Datei.

  6. Geben Sie die folgenden Daten ein:

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

    Geben Sie in Zelle A3 die folgende Formel ein:

    =AgeFunc(startdate,enddate)
    

    Das Startdatum ist ein Zellbezug auf Ihr erstes Datum (A1), und enddate ist ein Zellbezug auf Das zweite Datum (A2).

    Das Ergebnis sollte 58 sein.

Hinweis

Überprüfen Sie alle Daten vor dem 1.1.1900 auf Ihre Gültigkeit. Als Text eingegebene Datumsangaben werden von Excel nicht überprüft.

References

Weitere Informationen zur Verwendung des Beispielcodes in diesem Artikel finden Sie unter Ausführen von Beispielcode aus Knowledge Base-Artikeln in Office 2010.