Comment calculer les âges avant le 1/1/1900 dans Excel

Résumé

Bien que les formules de date Microsoft Excel puissent utiliser uniquement des dates entrées entre le 1/1/1900 et le 31/12/9999, vous pouvez utiliser une fonction Microsoft Visual Basic pour Applications personnalisée pour calculer l’âge (en années) d’une personne ou d’un élément qui a été créé avant le 1er janvier 1900.

Utiliser une macro pour calculer l’âge

Microsoft fournit des exemples de programmation à titre d’illustration uniquement, sans garantie expresse ou implicite. Cela inclut, sans y être limité, les garanties implicites de commercialisation et d’adaptation à un but en particulier. Cet article considère que vous connaissez le langage de programmation présenté et les outils utilisés pour créer et déboguer des procédures. Les techniciens du Support technique Microsoft peuvent vous expliquer les fonctionnalités d’une procédure particulière, mais ils ne peuvent pas modifier les exemples en vue de vous fournir des fonctionnalités supplémentaires ou de créer des procédures répondant à vos besoins spécifiques.

Excel entre les dates antérieures au 1/1/1900 sous forme de texte. Cette fonction fonctionne pour les dates entrées en tant que texte commençant au 1/1/0001, dates normales, et peut gérer les dates lorsque la date de début est antérieure à 1900 et la date de fin est postérieure à 1900. Pour utiliser la macro, procédez comme suit :

  1. Démarrez Excel. Affichez la feuille de calcul sur laquelle vous souhaitez utiliser la fonction .

  2. Appuyez sur Alt+F11 pour basculer vers le Rédacteur Visual Basic.

  3. Dans le menu Insertion, cliquez sur Module.

  4. Tapez le code suivant dans le module :

    ' 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. Enregistrez le fichier.

  6. Tapez les données suivantes :

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

    Dans la cellule A3, entrez la formule suivante :

    =AgeFunc(startdate,enddate)
    

    La date de début est une référence de cellule à votre première date (A1) et enddate une référence de cellule à votre deuxième date (A2).

    Le résultat doit être 58.

Remarque

Vérifiez la validité de toutes les dates antérieures au 1/1/1900. Les dates entrées sous forme de texte ne sont pas vérifiées par Excel.

References

Pour plus d’informations sur l’utilisation de l’exemple de code de cet article, voir Guide pratique pour exécuter un exemple de code à partir d’articles de la Base de connaissances dans Office 2010.