如何在 Excel 中计算 1/1/1/1900 之前的年龄 您所在的位置:网站首页 怎么用excel计算年龄大于18岁 如何在 Excel 中计算 1/1/1/1900 之前的年龄

如何在 Excel 中计算 1/1/1/1900 之前的年龄

2023-07-29 19:47| 来源: 网络整理| 查看: 265

如何在 Excel 中计算 1/1/1/1900 之前的年龄 项目 04/13/2023 适用于: Microsoft Excel 摘要

尽管 Microsoft Excel 日期公式只能使用在 1900 年 1 月 1 日至 9999 年 12 月 31 日之间输入的日期,但可以使用自定义 Microsoft Visual Basic for Applications 函数来计算某人的年龄 (年) 或在 1900 年 1 月 1 日之前首次创建的内容。

使用宏计算年龄

Microsoft 提供的编程示例仅用于进行说明,而不提供明示或默示担保。 这包括但不限于适销性或对特定用途的适用性的默示担保。 本文假设您熟悉正在演示的编程语言和用于创建和调试过程的工具。 Microsoft 支持工程师可以帮助解释特定过程的功能,但他们不会修改这些示例以提供新增功能或构建步骤以满足你的特定需要。

Excel 输入 1900 年 1 月 1 日之前的日期作为文本。 此函数适用于从 1/1/0001 开始作为文本输入的日期,即正常日期,并且可以在开始日期在 1900 年之前和结束日期在 1900 年之后处理日期。 若要使用宏,请执行以下步骤:

启动 Excel。 查看要在其中使用函数的工作表。

按 ALT+F11 切换到 Visual Basic 编辑器。

在“插入”菜单上,单击“模块”。

在模块中键入以下代码:

' 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

保存文件。

键入以下数据:

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

在单元格 A3 中,输入以下公式:

=AgeFunc(startdate,enddate)

startdate 是对第一个日期 (A1) 的单元格引用,而 enddate 是对第二个日期 (A2) 的单元格引用。

结果应为 58。

注意

检查 1900 年 1 月 1 日之前的所有日期是否有效。 Excel 不会检查以文本形式输入的日期。

参考

有关如何使用本文中的示例代码的详细信息,请参阅 如何从 Office 2010 中的知识库文章中运行示例代码。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有