|
发表于 2016-5-9 12:44
|
显示全部楼层
本楼为最佳答案
- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim theStr$, theLenth&, i&, theSum%, theNum%, a As Variant
- If Intersect(Target, Columns(4)) Is Nothing Then Exit Sub
- With Target
- If .Count > 1 Then Exit Sub
- If .Row < 12 Then Exit Sub
- theStr = .Value
- If theStr = "" Then Exit Sub
- theLenth = Len(theStr)
- If theLenth = 15 Or theLenth = 18 Then
- If theLenth = 18 Then
- If IsNumeric(Left(theStr, theLenth - 1)) Then
- a = VBA.Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
- For i = 1 To theLenth - 1
- theSum = theSum + CInt(Mid(theStr, i, 1)) * a(i - 1)
- Next i
- theNum = theSum Mod 11
- a = VBA.Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")
- If UCase(Right(theStr, 1)) <> a(theNum) Then
- MsgBox "校验码有误,请确认!", vbCritical, "错误"
- .Item(1).Select
- Application.SendKeys "{F2}"
- End If
- Else
- MsgBox "非阿拉伯数字串,请确认!", vbCritical, "错误"
- .Item(1).Select
- Application.SendKeys "{F2}"
- End If
- Else
- If Not IsNumeric(theStr) Then
- MsgBox "非阿拉伯数字串,请确认!", vbCritical, "错误"
- .Item(1).Select
- Application.SendKeys "{F2}"
- End If
- End If
- Else
- MsgBox "身份证长度不正确,请确认!", vbCritical, "错误"
- .Item(1).Select
- Application.SendKeys "{F2}"
- End If
- End With
- End Sub
复制代码 |
|