帮我转成vba自定义函数目前已有公式,但使用时不方便,请高手帮我转换成自定义函数:=if(LEN(B6)16,"贷款卡编码必须为16位",if(RIGHT(CONCATENATE("0",1+MOD(SUM((FIND(MID(B6,{1,2,3},1),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")-1)*{1,3,5})+SUM((FIND(MID(B6,{4,5,6,7,8,9,10,11,12,13,14},1),"0123456789")-1)*7,11,2,13,1,1,17,19,97,23,29}),97)),2)=MID(B6,15,2),"","贷款卡编码校验码错误"))
问题描述:
帮我转成vba自定义函数
目前已有公式,但使用时不方便,请高手帮我转换成自定义函数:=if(LEN(B6)16,"贷款卡编码必须为16位",if(RIGHT(CONCATENATE("0",1+MOD(SUM((FIND(MID(B6,{1,2,3},1),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")-1)*{1,3,5})+SUM((FIND(MID(B6,{4,5,6,7,8,9,10,11,12,13,14},1),"0123456789")-1)*7,11,2,13,1,1,17,19,97,23,29}),97)),2)=MID(B6,15,2),"","贷款卡编码校验码错误"))
答
Dim Arr0toZ$, Arr0to9$, ArrChar$(1 To 14), Arr135, Arr729
Dim i%, x!
If Len(ChNum) <> 16 Then
NumVerify = "贷款卡编码必须为16位"
Exit Function
End If
For i = 1 To 14
ArrChar(i) = Mid(ChNum, i, 1)
Next i
Arr0toZ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Arr0to9 = "0123456789"
Arr135 = Array("1", "3", "5")
Arr729 = Array("7", "11", "2", "13", "1", "1", "17", "19", "97", "23", "29")
For i = 1 To 3
x = x + (WorksheetFunction.Find(ArrChar(i), Arr0toZ) - 1) * Arr135(i - 1)
Next i
For i = 4 To 14
x = x + (WorksheetFunction.Find(ArrChar(i), Arr0to9) - 1) * Arr729(i - 4)
Next i
If Right(1 + x Mod 97, 2) <> Mid(ChNum, 15, 2) Then
NumVerify = "贷款卡编码校验码错误"
End If
End Function
ok
Function NumVerify(ChNum As String) As StringDim Arr0toZ$, Arr0to9$, ArrChar$(1 To 14), Arr135, Arr729
Dim i%, x!
If Len(ChNum) <> 16 Then
NumVerify = "贷款卡编码必须为16位"
Exit Function
End If
For i = 1 To 14
ArrChar(i) = Mid(ChNum, i, 1)
Next i
Arr0toZ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Arr0to9 = "0123456789"
Arr135 = Array("1", "3", "5")
Arr729 = Array("7", "11", "2", "13", "1", "1", "17", "19", "97", "23", "29")
For i = 1 To 3
x = x + (WorksheetFunction.Find(ArrChar(i), Arr0toZ) - 1) * Arr135(i - 1)
Next i
For i = 4 To 14
x = x + (WorksheetFunction.Find(ArrChar(i), Arr0to9) - 1) * Arr729(i - 4)
Next i
If Right(1 + x Mod 97, 2) <> Mid(ChNum, 15, 2) Then
NumVerify = "贷款卡编码校验码错误"
End If
End Function