|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 starprotector 于 2021-9-17 10:00 编辑
本人新手,前两天了解到身份证号码的校验规则,做了个自定义函数来校验
- Function IDCard(ID As String, Optional CheckMode As Boolean = False)
- Dim i As Byte, a
- IDCard = 0
- a = Array(0, 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
- If Len(ID) > 18 Or Len(ID) < 17 Or (CheckMode = False And Len(ID) = 17) Then
- IDCard = "位数有误!"
- Exit Function
- End If
- For i = 1 To 17
- IDCard = IDCard + Mid(ID, i, 1) * a(i)
- Next i
- IDCard = (12 - IDCard Mod 11) Mod 11
- If IDCard = 10 Then
- IDCard = "X"
- Else
- IDCard = IDCard & ""
- End If
-
- If CheckMode = False Then
- If IDCard = Right(ID, 1) Then
- IDCard = True
- Else
- IDCard = False
- End If
- End If
- End Function
复制代码 我还做个了函数说明,不过好像只能在2010及以后应用
- Sub IDCardDes()
- Dim ArgDes(1 To 2) As String
- ArgDes(1) = "18位身份证号码;计算校验码时可以只有前17位"
- ArgDes(2) = "校验模式,逻辑值 False(默认) 或 True。前者校验18位身份证号码,输出逻辑值校验结果;后者根据前17位号码计算第18位校验码"
- Application.MacroOptions Macro:="IDCard", _
- Description:="校验身份证号码(18位)", _
- Category:=14, _
- ArgumentDescriptions:=ArgDes()
- End Sub
复制代码 效果展示:
上图是检验身份证号码是否能通过校验。
下图是根据前17位计算出第18位校验码
|
评分
-
1
查看全部评分
-
|