excel身份证验证(附带防止粘贴导致校验失效的函数)

Posted guozhigang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel身份证验证(附带防止粘贴导致校验失效的函数)相关的知识,希望对你有一定的参考价值。

一、检验函数

=IF(LEN(A1)=18,MID("10X98765432",MOD(SUMPRODUCT(MID(A1,ROW(INDIRECT("1:17")),1)*2^(18-ROW(INDIRECT("1:17")))),11)+1,1)=RIGHT(A1))----18位校验

二、防止粘贴导致校验失效的函数

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim rng As Range
   Dim c As String
    For Each rng In Target
    If Not rng.Validation.Value Then
        Application.Undo
        Dim msg As String
        msg = "粘贴的数据不符合校验规则:位置在第" & rng.Row & "行,第" & getColumnName(rng.column) & "列,请仔细检查"
        MsgBox prompt:=msg, Title:="输入提示"
    Exit For
    End If
Next
End Sub
 
 
Private Function getColumnName(column As Integer) As String
    Dim alphabet(26) As String
    alphabet(0) = "A"
    alphabet(1) = "B"
    alphabet(2) = "C"
    alphabet(3) = "D"
    alphabet(4) = "E"
    alphabet(5) = "F"
    alphabet(6) = "G"
    alphabet(7) = "H"
    alphabet(8) = "I"
    alphabet(9) = "J"
    alphabet(10) = "K"
    alphabet(11) = "L"
    alphabet(12) = "M"
    alphabet(13) = "N"
    alphabet(14) = "O"
    alphabet(15) = "P"
    alphabet(16) = "Q"
    alphabet(17) = "R"
    alphabet(18) = "S"
    alphabet(19) = "T"
    alphabet(20) = "U"
    alphabet(21) = "V"
    alphabet(22) = "W"
    alphabet(23) = "X"
    alphabet(24) = "Y"
    alphabet(25) = "Z"
 
    
    If (column < 27) Then
        getColumnName = alphabet(column - 1)
    Else
        Dim i, j As Integer
        i = column  26
        j = column Mod 26
        If (i < 26) Then
            getColumnName = alphabet(i - 1) & alphabet(j - 1)
        Else
            getColumnName = column
        End If
            
    End If
    
End Function

 

以上是关于excel身份证验证(附带防止粘贴导致校验失效的函数)的主要内容,如果未能解决你的问题,请参考以下文章

下载软件后为啥要验证md5?

为啥文本文件中的身份证号码粘贴到EXCEL中时将变为科学计数法表示

weblogic下包冲突 导致@Valid失效 不能正确校验表单 分析与解决

为啥我报考公务员总是出现页面失效请重新登录

js 精确验证身份证(地址编码出生日期校验位验证)

校验身份证号+验证码校验(vue+element)