Condividi tramite


Come calcolare le età prima del 1/1/1900 in Excel

Riepilogo

Anche se le formule di data di Microsoft Excel possono usare solo date immesse tra il 1/1/1/1900 e il 31/12/9999, è possibile usare una funzione microsoft Visual Basic, Applications Edition personalizzata per calcolare l'età (in anni) di un utente o di un elemento creato prima del 1° gennaio 1900.

Usare la macro per calcolare l'età

Microsoft fornisce esempi di programmazione a scopo puramente illustrativo, senza alcuna garanzia di qualsiasi tipo, sia espressa che implicita, ivi incluse, senza limitazioni, le garanzie implicite di commerciabilità o idoneità per uno scopo particolare. In questo articolo si presuppone che l'utente conosca il linguaggio di programmazione in questione e gli strumenti utilizzati per creare ed eseguire il debug delle procedure. Gli esperti Microsoft sono autorizzati a fornire spiegazioni in merito alla funzionalità di una particolare routine, ma in nessun caso a modificare questi esempi per fornire funzionalità aggiuntive o a creare routine atte a soddisfare specifiche esigenze.

Excel immette come testo le date precedenti al 1/1/1/1900. Questa funzione funziona per le date immesse come testo che inizia con 1/1/0001, date normali e può gestire le date quando la data di inizio è precedente al 1900 e la data di fine è successiva al 1900. Per usare la macro, seguire questa procedura:

  1. Avviare Excel. Visualizzare il foglio di lavoro in cui si vuole usare la funzione.

  2. Premere ALT+F11 per passare alla Editor di Visual Basic.

  3. Nel menu Inserisci, fare clic su Modulo.

  4. Digitare il codice seguente nel modulo:

    ' 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. Salvare il file.

  6. Digitare i dati seguenti:

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

    Nella cella A3 immettere la formula seguente:

    =AgeFunc(startdate,enddate)
    

    Startdate è un riferimento di cella alla prima data (A1) e enddate è un riferimento di cella alla seconda data (A2).

    Il risultato deve essere 58.

Nota

Verificare la validità di tutte le date precedenti al 1/1/1/1900. Le date immesse come testo non vengono controllate da Excel.

Riferimenti

Per altre informazioni su come usare il codice di esempio in questo articolo, vedere Come eseguire codice di esempio dagli articoli della Knowledge Base in Office 2010.