在Excel中,你可以使用函数来计算根据身份证号码日期部分计算年龄。例如,假设身份证号码位于A1单元格,你可以通过以下步骤计算年龄:
1. 创建一个名为"GetBirthDate"的自定义函数,在VBA编辑器中输入以下代码:

Function GetBirthDate(id As String) As Date
Dim strYear As String
Dim strMonth As String
Dim strDay As String
If Len(id) = 18 Then
strYear = Left(id, 4)
strMonth = Mid(id, 5, 2)
strDay = Mid(id, 7, 2)
ElseIf Len(id) = 15 Then
strYear = "19" & Left(id, 2)
strMonth = Mid(id, 4, 2)
strDay = Mid(id, 6, 2)
Else
GetBirthDate = ""
Exit Function
End If
GetBirthDate = DateSerial(CInt(strYear), CInt(strMonth), CInt(strDay))
End Function
2. 回到Excel工作表,在B1单元格输入下列公式并按下回车键:

=DATEDIF(GetBirthDate(A1), TODAY(), "y")
这个公式利用了之前创建的自定义函数GetBirthDate将身份证号码转换为生日,并使用DATEDIF函数计算从生日到今天的完整年数。
注意:在使用自定义函数之前,你需要确保启用了VBA宏。可以通过依次点击"文件" -> "选项" -> "宏",并勾选"启用所有宏"来启用VBA宏。

通过以上步骤,你可以在B1单元格中得到身份证号码对应的年龄。如果你需要将结果保持为整数,可以将公式改为:
=INT(DATEDIF(GetBirthDate(A1), TODAY(), "y"))
希望这些信息对你有所帮助!