Beräkna ålder före 1900-01-01 i Excel
Anteckning
Office 365 ProPlus byter namn till Microsoft 365-appar för företag. Mer information om den här ändringen finns i det här blogginlägget.
Sammanfattning
Även om Microsoft Excel datumformel endast kan använda datum som anges mellan 1900-01-01 och 9999-12-31 kan du använda en anpassad Microsoft Visual Basic for Applications-funktion för att beräkna åldern (i år) för någon eller något som först skapades före den 1 januari 1900.
Beräkna ålder med makrot
Microsoft tillhandahåller programmeringsexempel enbart i förklarande syfte och gör inga utfästelser, varken uttryckligen eller underförstått. Detta omfattar men begränsas inte till underförstådd garanti för säljbarhet eller lämplighet för ett visst syfte. I denna artikel förutsätts att du känner till det programmeringsspråk som demonstreras och de verktyg som används för att skapa och felsöka procedurer. Microsofts supportexperter kan hjälpa till att förklara funktionerna i en viss procedur, men de kommer inte att ändra dessa exempel för att tillhandahålla extra funktioner eller konstruera procedurer för att uppfylla dina specifika behov.
Excel datum före 1900-01-01 som text. Den här funktionen fungerar för datum som anges som text som börjar med 01.01.001, normaldatum och kan hantera datum när startdatumet inträffar före 1900 och slutdatumet inträffar efter 1900. Gör så här om du vill använda makrot:
Starta Excel. Visa det kalkylblad där du vill använda funktionen.
Tryck på ALT+F11 för att växla till Visual Basic Editor.
Klicka på Modul på Infoga-menyn.
Skriv in följande kod i modulen:
' 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 FunctionSpara filen.
Skriv in följande data:
A1 01/01/1887 A2 02/02/1945Ange följande formel i cell A3:
=AgeFunc(startdate,enddate)Startdatumet är en cellreferens till ditt första datum (A1) och slutdatum är en cellreferens till det andra datumet (A2).
Resultatet ska vara 58.
Anteckning
Kontrollera giltigheten för alla datum före 1900-01-01. Datum som angetts som text kontrolleras inte av Excel.
Referenser
Mer information om hur du använder exempelkoden i den här artikeln finns i Köra exempelkod från Knowledge Base-artiklar i Office 2010.