Leeftijden berekenen vóór 1-1-1900 in Excel
Samenvatting
Hoewel datumformules in Microsoft Excel alleen datums kunnen gebruiken die zijn ingevoerd tussen 1-1-1900 en 31-12-9999, kunt u een aangepaste functie Microsoft Visual Basic for Applications gebruiken om de leeftijd (in jaren) te berekenen van iemand of iets dat voor het eerst is gemaakt vóór 1 januari 1900.
Macro gebruiken om de leeftijd te berekenen
Microsoft verstrekt programmeervoorbeelden alleen ter illustratie, zonder expliciete of impliciete garantie. daaronder mede begrepen, maar niet beperkt tot impliciete garanties met betrekking tot de verkoopbaarheid en/of geschiktheid voor een bepaald doel. In dit artikel wordt ervan uitgegaan dat u bekend bent met de programmeertaal VBScript, alsmede met de hulpprogramma's waarmee procedures worden gemaakt en waarmee fouten in procedures worden opgespoord. U kunt desgewenst contact opnemen met Microsoft Product Support Services voor uitleg over de functie van een bepaalde procedure. Microsoft Product Support Services is echter niet bereid de voorbeelden aan te passen om extra functies toe te voegen of om procedures te maken die aan uw specifieke eisen voldoen.
In Excel worden datums vóór 1-01-1900 als tekst ingevoerd. Deze functie werkt voor datums die worden ingevoerd als tekst vanaf 1-1-0001, normale datums, en kan datums verwerken wanneer de begindatum vóór 1900 is en de einddatum na 1900 valt. Voer de volgende stappen uit om de macro te gebruiken:
Start Excel. Bekijk het werkblad waarop u de functie wilt gebruiken.
Druk op Alt+F11 om over te schakelen naar de Visual Basic-Editor.
Klik op Module in het menu Invoegen.
Typ de volgende code in de 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
Sla het bestand op.
Typ de volgende gegevens:
A1 01/01/1887 A2 02/02/1945
Voer in cel A3 de volgende formule in:
=AgeFunc(startdate,enddate)
De begindatum is een celverwijzing naar uw eerste datum (A1) en de einddatum is een celverwijzing naar uw tweede datum (A2).
Het resultaat moet 58 zijn.
Opmerking
Controleer alle datums voor 1-1-1900 op geldigheid. Datums die als tekst zijn ingevoerd, worden niet gecontroleerd door Excel.
Verwijzingen
Zie Voorbeeldcode uitvoeren vanuit Knowledge Base-artikelen in Office 2010 voor meer informatie over het gebruik van de voorbeeldcode in dit artikel.
Feedback
https://aka.ms/ContentUserFeedback.
Binnenkort beschikbaar: In de loop van 2024 zullen we GitHub-problemen geleidelijk uitfaseren als het feedbackmechanisme voor inhoud en deze vervangen door een nieuw feedbacksysteem. Zie voor meer informatie:Feedback verzenden en weergeven voor