alt+f11进宏编辑器,sheet1上右键插入模块,贴入以下代码,关闭宏编辑器后,在你需要校验位的地方(比如卡号在A1),那就输入=myVerify(a1)
Option Explicit
Function myVerify(ref As Range) As Integer
Dim r As String, temp As String
Dim i As Integer, s As Long, temp2 As Integer '第一步乘积会超出整型范围,故定义为long
r = Trim(ref.Text)
If Len(r) <> 14 Then
myVerify = 0
MsgBox (ref.Address & "中有效位数不为14位")
Return
End If
temp = ""
For i = 1 To 13 Step 2 'temp为1至13位字符
temp = temp & Mid(r, i, 1)
Next i
temp2 = 0
For i = 2 To 14 Step 2 'temp2为2至14位合计
temp2 = temp2 + Val(Mid(r, i, 1))
Next i
s = Val(temp) * 2
temp = Right(String(0, "0") & s, 7) '补0取后七位,第一步完成
For i = 1 To 7 '第二、三步,结果存于temp2
temp2 = temp2 + Val(Mid(temp, i, 1))
Next i
Dim v As Integer '最后校验位
v = 0
If Val(Right(temp2, 1)) > 0 Then v = 10 - Val(Right(temp2, 1))
myVerify = v
End Function
有挑战, 本小仙给你搞定,先占楼
=LOOKUP(--RIGHT(SUM(--MID(RIGHT(2*(LEFT(A1,1)&MID(A1,3,1)&MID(A1,5,1)&MID(A1,7,1)&MID(A1,9,1)&MID(A1,11,1)&MID(A1,13,1)),7),{1;2;3;4;5;6;7},1))+SUM(--MID(A1,{2;4;6;8;10;12;14},1)),1),{0,1,2,3,4,5,6,7,8,9},{0,9,8,7,6,5,4,3,2,1})
我晕, 我觉得自己定义个 自定义函数好些, 这个太长了
A1:88210123456789
校验码:
=RIGHT(10-RIGHT(SUM(--MID(RIGHT(TEXT(SUM(MID(A1,{1,3,5,7,9,11,13},1)*10^{6,5,4,3,2,1,0})*2,"0000000"),7),{1,2,3,4,5,6,7},1))+SUM(--MID(A1,{2,4,6,8,10,12,14},1))))
=RIGHT(10-RIGHT(SUM(--MID(TEXT(RIGHT(SUM(MID(A1,ROW($1:$7)*2-1,1)*10^(7-ROW($1:$7)))*2,7),REPT(0,7)),ROW($1:$7),1),--MID(A1,ROW($1:$7)*2,1))))
值得学习