|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我原先也写过一个,你参考一下——- Function s15to18(IdStr) As String '身份证号码校验
- On Error Resume Next
- Dim wi As Variant, ji As Variant, sum%, i%, intMsg%
- wi = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
- ji = Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")
- sum = 0
- If Len(IdStr) = 15 Then
- If Not IsNumeric(IdStr) Then
- MsgBox "15位身份证号码中有非数字字符!"
- Exit Function
- End If
- IdStr = Left(IdStr, 6) & "19" & Right(IdStr, 9)
- ElseIf Len(IdStr) = 18 Then
- If Not (IsNumeric(Mid(IdStr, 1, 17)) And (IsNumeric(Right(IdStr, 1)) Or Right(IdStr, 1) = "X" Or Right(IdStr, 1) = "x")) Then
- MsgBox "18位身份证号码中有非数字字符!"
- Exit Function
- End If
- Else
- MsgBox "身份证号码位数不对,请检查!"
- Exit Function
- End If
- If IsError(DateValue(Mid(IdStr, 7, 4) & "-" & Mid(IdStr, 11, 2) & "-" & Mid(IdStr, 13, 2))) Then
- MsgBox "身份证号码中日期信息有误!"
- Exit Function
- End If
- For i = 0 To UBound(wi)
- sum = sum + Mid(IdStr, i + 1, 1) * wi(i)
- Next i
- If Len(IdStr) = 17 Then
- s15to18 = IdStr & ji(sum Mod 11)
- Else
- If ji(sum Mod 11) <> Right(IdStr, 1) Then
- intMsg = MsgBox("18位身份证号码中的校验码错误!" & vbCrLf & "您要输入的是:" & Mid(IdStr, 1, 17) & ji(sum Mod 11) & "吗?", vbYesNo)
- If intMsg = vbYes Then
- s15to18 = Mid(IdStr, 1, 17) & ji(sum Mod 11)
- Else
- Exit Function
- End If
- Else
- s15to18 = IdStr
- End If
- End If
- End Function
复制代码 |
|