如何用ExcelVBA进行身份证号码真伪验证

2024-10-16 11:57:10

1、打开一张Excel表格或者新建一张Excel表格,以新建一张Excel表格为例,新建并打开Excel表格。

如何用ExcelVBA进行身份证号码真伪验证

2、进行初始数据的编辑,可以去网上找测试用的身份证号码,这些只是满足身份证的计算规则。然后进行代码的编辑,如果正确就在F或者G列相应打勾,否则就打叉。

如何用ExcelVBA进行身份证号码真伪验证

4、首先点击菜单: 插入——模块代码编辑分两大部分:身份证是否正确和性别是否正确输入以下代码Sub 身份验证()Dim arr1(), arr2(), t, leftString, sarr1 = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) '系数arr2 = Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2") '对应的结果s = 0For Each Rng In Range("d2", Cells(Rows.Count, "d").End(xlUp)) s = 0 If Len(Rng.Text) = 18 Then For i = 1 To 17 t = Mid(Rng.Text, i, 1) '取出每位数 s = s + t * arr1(i - 1) '求和 Next i s = s Mod 11 '取余数 If arr2(s) = Mid(Rng.Text, 18, 1) Then '判断是否与最后一位相等 Range("g" & Rng.Row).Value = "√" Else Range("g" & Rng.Row).Value = "×" Range("f" & Rng.Row).Value = "×" End If Else Range("g" & Rng.Row).Value = "×" Range("f" & Rng.Row).Value = "×" '这是新加的一行,以前有点错误 End If leftString = Mid(Rng.Text, Len(Rng.Text) - 1, 1) If (leftString Mod 2) = 1 Then If Range("e" & Rng.Row).Value = "男" Then Range("f" & Rng.Row).Value = "√" Else Range("f" & Rng.Row).Value = "×" End If Else If Range("e" & Rng.Row).Value = "女" Then Range("f" & Rng.Row).Value = "√" Else Range("f" & Rng.Row).Value = "×" End If End IfNextEnd Sub

如何用ExcelVBA进行身份证号码真伪验证如何用ExcelVBA进行身份证号码真伪验证如何用ExcelVBA进行身份证号码真伪验证如何用ExcelVBA进行身份证号码真伪验证

7、点击文件——另存为———启用宏的工作簿,然后改一下名称和路径,其他可能不能保存代码

如何用ExcelVBA进行身份证号码真伪验证
猜你喜欢