Excel自定义函数IDCardInfo身份证识别,一个函数身份证号码中生日/年龄/性别/生肖/星座,还能自动验真
- 2026-12-01 01:42:40
Excel自定义函数IDCardInfo身份证识别,一个函数身份证号码中生日/年龄/性别/生肖/星座,还能自动验真如果你是工地上的劳资专员、办公室里的人事或者是教培机构的朋友,总免不了跟身份证号码打交道。 今天跟大家分享一个Excel/WPS的VBA自定义函数IDCardInfo,无需复杂的嵌套函数也无需手动拆分身份证号码一一计算, 一、函数核心功能详解 二、函数语法与参数说明 函数语法: 参数说明:
三、实操案例 四、VBA代码
六大功能合一:生日、周岁年龄、性别、生肖、星座、真伪验证一键切换
严格真伪校验:18位身份证校验码算法校验+生日日期合法性双重验证
=IDCardInfo (单元格,[模式1生日2年龄3性别4生肖5星座6有效性])已关注
关注
重播 分享 赞
Function IDCardInfo(单元格 As Variant, Optional 模式1生日2年龄3性别4生肖5星座6有效性 As Integer = 1) As VariantDim id As String, isValid As BooleanDim birthDate As Date, age As IntegerDim gender As String, zodiac As String, constellation As StringDim i As Integer, sum As LongDim weights As Variant, checkCodes As StringDim prefix As String, checkCode As String, calcCheck As StringDim y As String, m As String, d As StringDim zodiacArr As Variantid = CStr(单元格)id = Trim(id)isValid = Falseweights = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)checkCodes = "10X98765432"zodiacArr = Array("鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪")If Len(id) = 15 Then' 15位:全数字验证If IsNumeric(id) Theny = "19" & Mid(id, 7, 2)m = Mid(id, 9, 2)d = Mid(id, 11, 2)If IsDate(y & "/" & m & "/" & d) ThenbirthDate = CDate(y & "/" & m & "/" & d)isValid = Truegender = IIf(CInt(Mid(id, 15, 1)) Mod 2 = 1, "男", "女")End IfEnd IfElseIf Len(id) = 18 Then' 18位:前17位数字 + 校验码验证prefix = Left(id, 17)checkCode = UCase(Right(id, 1))If IsNumeric(prefix) And (checkCode Like "[0-9]" Or checkCode = "X") Thensum = 0For i = 1 To 17sum = sum + CInt(Mid(prefix, i, 1)) * weights(i - 1)Next icalcCheck = Mid(checkCodes, (sum Mod 11) + 1, 1)If checkCode = calcCheck Theny = Mid(id, 7, 4)m = Mid(id, 11, 2)d = Mid(id, 13, 2)If IsDate(y & "/" & m & "/" & d) ThenbirthDate = CDate(y & "/" & m & "/" & d)isValid = Truegender = IIf(CInt(Mid(id, 17, 1)) Mod 2 = 1, "男", "女")End IfEnd IfEnd IfEnd If' 无效时返回If Not isValid ThenIf 模式1生日2年龄3性别4生肖5星座6有效性 = 6 ThenIDCardInfo = "无效"ElseIDCardInfo = "身份证无效"End IfExit FunctionEnd If' 年龄(周岁)age = Year(Date) - Year(birthDate)If Month(Date) < Month(birthDate) Or (Month(Date) = Month(birthDate) And Day(Date) < Day(birthDate)) Thenage = age - 1End If' 生肖zodiac = zodiacArr((Year(birthDate) - 1900) Mod 12)' 星座Select Case Month(birthDate)Case 1constellation = IIf(Day(birthDate) >= 20, "水瓶座", "摩羯座")Case 2constellation = IIf(Day(birthDate) >= 19, "双鱼座", "水瓶座")Case 3constellation = IIf(Day(birthDate) >= 21, "白羊座", "双鱼座")Case 4constellation = IIf(Day(birthDate) >= 20, "金牛座", "白羊座")Case 5constellation = IIf(Day(birthDate) >= 21, "双子座", "金牛座")Case 6constellation = IIf(Day(birthDate) >= 22, "巨蟹座", "双子座")Case 7constellation = IIf(Day(birthDate) >= 23, "狮子座", "巨蟹座")Case 8constellation = IIf(Day(birthDate) >= 23, "处女座", "狮子座")Case 9constellation = IIf(Day(birthDate) >= 23, "天秤座", "处女座")Case 10constellation = IIf(Day(birthDate) >= 24, "天蝎座", "天秤座")Case 11constellation = IIf(Day(birthDate) >= 23, "射手座", "天蝎座")Case 12constellation = IIf(Day(birthDate) >= 22, "摩羯座", "射手座")End Select'返回结果Select Case 模式1生日2年龄3性别4生肖5星座6有效性Case 1IDCardInfo = Format(birthDate, "yyyy-mm-dd")Case 2IDCardInfo = ageCase 3IDCardInfo = genderCase 4IDCardInfo = zodiacCase 5IDCardInfo = constellationCase 6IDCardInfo = "有效"Case ElseIDCardInfo = "身份证无效"End SelectEnd Function
五、使用方法
打开 Excel,按 Alt+F11打开 VBA 编辑器;右键工作表→插入→模块; 复制本文的 VBA 函数代码,粘贴到模块中; 关闭 VBA 编辑器,返回表格即可直接使用公式!
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。