Function ID(num) Dim X(17) '储存身份证号码分割后的每位字符 Dim Y() '储存计算相乘的系数 Dim LastNum() '储存身份证最后一位验证码 '储存身份证前17位For i = 0 To 16 X(i) = Mid(num, i + 1, 1) Next '如果第18位是字母X或x,将数字10储存到X(17)中 '如果第18位是数字,直接储存到X(17)中 '如果是其他情况,什么都不做 If Mid(num, 18, 1) = "X" Or Mid(num, 18, 1) = "x" Then X(17) = 10 ElseIf IsNumeric(Mid(num, 18, 1)) Then X(17) = Mid(num, 18, 1) End If 'Y中最后一位11是取模的除数,其余是相乘系数 Y = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 11) LastNum = Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2) '判断位数,18位且前17位都是数字才可能正确,再计算校验码判断 If Len(num) = 18 And Application.WorksheetFunction.IsNumber(Mid(num, 1, 17)) Then Sum = 0 For i = 0 To 16 Sum = Sum + X(i) * Y(i) Next Code = Sum Mod Y(17) If LastNum(Code) - X(17) = 0 Then ID = "正确" Else ID = "请检查身份证号码!" End If '非18位情况身份证号码错误 Else ID = "请检查身份证号码!" End If End Function