|
<p>身份证号码的处理确实是一个比较烦人的东西,不过通过以下三步,相信你可以很好的驾驭它:</p><p>1、定义数据有效性,将待输入身份证号码的区域设置数据的有效性,数据-》有效性-》设置-》允许-》自定义-》公式“=OR(LEN(C9)=15,LEN(C9)=18)”<br/>接着,出错-》标题“身份证号码位数不正确”-》错误信息“请检查身份证号码的位数,必须15位或18位!”</p><p>2、正确设置单元格格式,把身份证号码所在列的单元格格式设置成“文本”,或者确保你输入时先输入西文半角的单引号“'”,这样会把身份证号强制识别为文本格式</p><p>3、增加辅助列,运用自定义函数进行检查,=CheckID(<em><font color="#808080">[单元格引用]</font></em>)</p><p>Alt+F11进入VBE编辑器,插入—》模块,增加如下代码:</p><p>Option Explicit</p><p>Function CheckID(IdStr) As String '身份证号码校验<br/>On Error Resume Next<br/>Dim wi As Variant, ji As Variant, sum As Integer, i%, intMsg%<br/>wi = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)<br/>ji = Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")<br/>sum = 0<br/>If Len(IdStr) = 15 Then<br/> If Not IsNumeric(IdStr) Then<br/> MsgBox "15位身份证号码中有非数字字符!"<br/> Exit Function<br/> End If<br/> IdStr = Left(IdStr, 6) & "19" & Right(IdStr, 9)<br/>ElseIf Len(IdStr) = 18 Then<br/> If Not (IsNumeric(Mid(IdStr, 1, 17)) And (IsNumeric(Right(IdStr, 1)) Or Right(IdStr, 1) = "X" Or Right(IdStr, 1) = "x")) Then<br/> MsgBox "18位身份证号码中有非数字字符!"<br/> Exit Function<br/> End If<br/>Else '不是15位或者18位的话返回空值<br/> Exit Function<br/>End If<br/>If IsError(DateValue(Mid(IdStr, 7, 4) & "-" & Mid(IdStr, 11, 2) & "-" & Mid(IdStr, 13, 2))) Then<br/> MsgBox "身份证号码中日期信息有误!"<br/> Exit Function<br/>End If</p><p>For i = 0 To UBound(wi)<br/> sum = sum + Mid(IdStr, i + 1, 1) * wi(i)<br/>Next i</p><p>If Len(IdStr) = 17 Then<br/> CheckID = IdStr & ji(sum Mod 11)<br/>Else<br/> If ji(sum Mod 11) <> Right(IdStr, 1) Then<br/> intMsg = MsgBox("18位身份证号码中的校验码错误!" & vbCrLf & "您要输入的是:" & Mid(IdStr, 1, 17) & ji(sum Mod 11) & "吗?", vbYesNo)<br/> If intMsg = vbYes Then<br/> CheckID = Mid(IdStr, 1, 17) & ji(sum Mod 11)<br/> Else<br/> Exit Function<br/> End If<br/> Else<br/> CheckID = IdStr<br/> End If<br/>End If<br/>End Function</p><p></p> |
|