Excel利用出生日期来计算年龄的两个自定义函数
时间:2023-09-15 09:36:38
如果要用出生日期来计算年龄,可以用自定义函数来完成。在VBA编辑器中单击菜单“插入→模块”,并将下面的代码放入其中,以下两段代码都是按虚岁计算的,任选一种即可。
方法一:
Function Age(BirthDate As Date)
Select Case Month(Date)
Case Is < Month(BirthDate)
Age = Year(Date) - Year(BirthDate)
Case Is = Month(BirthDate)
If Day(Date) >= Day(BirthDate) Then
Age = Year(Date) - Year(BirthDate) + 1
Else
Age = Year(Date) - Year(BirthDate)
End If
Case Is > Month(BirthDate)
Age = Year(Date) - Year(BirthDate) + 1
End Select
End Function
方法二:将一年按365.25天计算,在极少数情况下可能会出错。
Function Age(BirthDate As Date)
Age = Int((Date - BirthDate) / 365.25) + 1
End Function
使用方法:假如出生日期在A1单元格中,如“1971-12-1”,在B1单元格中计算年龄,则在B1单元格中输入公式
=age(A1)
B1单元格返回计算结果为38。请确保A1单元格为日期格式,否则会出现“#VALUE!”错误。
标签:Excel,利用,出生日期,来,计算,年龄,的,两个
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PPT如何快速绘制一个六边形循环图 需要利用六边形的同比缩放或SmartArt转换功能搞定
2022-07-30 21:38:55
![](https://img.aspxhome.com/file/2023/10/a352540_0s.jpg)
windows10系统ghost在哪下载?windows10系统ghost下载教程
2023-11-08 09:28:38
![](https://img.aspxhome.com/file/2023/6/48056_0s.jpg)
Win10系统启用隐藏通知中心的全新方法
2023-09-07 00:19:52
![](https://img.aspxhome.com/file/2023/29/a253470_0s.png)
Word如何快速删除多余空行?
2022-07-10 18:51:19
![](https://img.aspxhome.com/file/2023/6/32136_0s.jpg)
0x000007b电脑蓝屏怎么办_0x000007b蓝屏解决教程
2023-07-27 21:42:07
![](https://img.aspxhome.com/file/2023/4/a305575_0s.jpg)
新U盘格式化的具体步骤
2022-01-28 21:23:39
![](https://img.aspxhome.com/file/2023/4/a307227_0s.jpg)
电脑版wps文档如何删除页脚中的省略号
2022-10-20 02:45:06
![](https://img.aspxhome.com/file/2023/8/a182998_0s.png)
Win10如何隐藏任务栏U盘图标?
2023-09-23 18:07:19
![](https://img.aspxhome.com/file/2023/26/a225911_0s.jpg)
Win10一周年更新预览版14316中怎么设置活跃时间?
2023-12-02 23:17:10
![](https://img.aspxhome.com/file/2023/29/a261634_0s.png)
word怎样去掉页码
2023-08-15 16:06:11
vscode怎么设置代码颜色?Vscode编辑器配色设置方法
2023-06-27 09:29:01
![](https://img.aspxhome.com/file/2023/30/a268804_0s.jpg)
wps中如何设置标题样式
2023-11-19 15:00:49
Win10开机黑屏时间长如何解决?
2023-11-14 10:56:04
![](https://img.aspxhome.com/file/2023/3/49343_0s.png)
win7系统语言栏不见了怎么调出来?
2023-10-14 04:46:09
![](https://img.aspxhome.com/file/2023/30/a271161_0s.jpg)
Win7系统右下角小旗子强大功能介绍
2023-10-07 14:53:45
![](https://img.aspxhome.com/file/2023/29/a256436_0s.jpg)
批量删除Excel2007表中的空值
2023-05-12 18:32:08
![](https://img.aspxhome.com/file/2023/6/a143146_0s.jpg)
更改excel文件扩展名的方法
2022-02-02 00:54:16
![](https://img.aspxhome.com/file/2023/0/a167850_0s.gif)
实现word文字与表格互换的两种方法
2023-05-05 22:10:21
word页码设置第几页共几页格式
2023-02-16 08:41:26
![](https://img.aspxhome.com/file/2023/3/35753_0s.png)
深度技术win7系统光盘怎么安装_深度技术win7系统光盘安装教程
2023-09-17 04:47:53
![](https://img.aspxhome.com/file/2023/4/a313221_0s.png)