|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Rem **********************************************************************************************
- Rem 函数名: CheckSFZCardID
- Rem 函数功能: 检查身份证号码是否合规则
- Rem 返回值: 返回 布尔类型
- Rem 参数1: CardID 字符类型 身份证号码
- Rem 参数2: StrOUT 文本类型 要什么结果: 出生日期,性别,对比结果 默认是=对比结果
- Rem 参数3: TiShi 布尔类型 是否显示错误信息
- Rem 使用方法: MsgBox CheckCardID_SFZ(CardID:=StrID, StrOUT:="" ,TiShi:=True)
- Rem 整理:北极狐工作室 QQ:14885553
- Rem ***********************************************************************************************
- Public Function CheckCardID_SFZ(ByVal CardID As String, Optional StrOUT As String = "对比结果", Optional TiShi As Boolean = True)
- Dim Exception, StrAddSF As String
- Dim ARR, BRR, SHUZI, ZAD
- Dim WEI, YAN1, YAN2 As Integer
- Dim DATEX As String
- Dim BOOL As Boolean
-
- Application.Volatile '//自动计算自定义函数
- BOOL = True '//假设正确
- Exception = ""
-
- If StrOUT = "" Then StrOUT = "对比结果" '//性别,出生日期
- Set ZAD = CreateObject("Scripting.Dictionary")
-
- If Len(CardID) <> 18 Then
- Rem 限定身份证号是18位
- BOOL = False
- Exception = Exception & vbCrLf & "身份证号必须是:18位"
- Else
- Rem 省份验证
- StrAddSF = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"
- If InStr(StrAddSF, Mid(CardID, 1, 2)) = 0 Then
- Exception = Exception & vbCrLf & "省份部分有误"
- BOOL = False
- End If
-
- Rem 校验前17位是否为数字/检验第18位校验码是否正确
- ARR = Array("7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2")
- BRR = Array("1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2")
- For WEI = 1 To 17
- SHUZI = Mid(CardID, WEI, 1)
- If IsNumeric(SHUZI) = False Then
- BOOL = False
- Exception = Exception & vbCrLf & "您可能输入了非法字符"
- Else
- YAN1 = YAN1 + SHUZI * ARR(WEI - 1)
- End If
- Next
-
- If Exception = "" Then
- Rem 校验码正确
- YAN2 = YAN1 Mod 11
- If UCase(Mid(CardID, 18)) <> UCase(BRR(YAN2)) Then
- BOOL = False
- Exception = Exception & vbCrLf & "校验码不符"
- End If
-
- Rem '限定出生日期范围为1920.1.1-2100.1.1/消息框提示再次确认出生日期、性别
- DATEX = Mid(CardID, 7, 4) & "-" & Mid(CardID, 11, 2) & "-" & Mid(CardID, 13, 2)
- If IsDate(DATEX) = False Then
- BOOL = False
- Exception = Exception & vbCrLf & "输入的出生时间可能有误"
- Else
- DATEX = Format(DATEX, "yyyy-MM-dd")
- If DateDiff("D", "1901-01-01", DATEX) > 0 And DateDiff("D", "2101-01-01", DATEX) < 0 Then
- ZAD("出生日期") = DATEX
- If InStr("13579", Mid(CardID, 17, 1)) > 0 Then
- ZAD("性别") = "男"
- Else
- ZAD("性别") = "女"
- End If
- Else
- BOOL = False
- Exception = Exception & vbCrLf & "输入的出生时间超范围"
- End If
- End If
- End If
- End If
-
- Rem 输出结果
- If Exception = "" Then
- Rem 无错误,正常输出
- ZAD("对比结果") = BOOL
- CheckCardID_SFZ = ZAD(StrOUT)
- Else
- Rem 如果有错误
- If TiShi = True Then
- Rem 直接输出错误信息
- CheckCardID_SFZ = Exception
- Else
- Rem 根据要求选择输出值
- Select Case StrOUT
- Case "对比结果"
- CheckCardID_SFZ = False
- Case "性别"
- CheckCardID_SFZ = ""
- Case "出生日期"
- CheckCardID_SFZ = ""
- Case Else
- CheckCardID_SFZ = False
- End Select
- End If
- End If
- End Function
复制代码 |
|