如何修改银行账号密钥计算代码,取右侧10位数字计算密钥?
修改后的VBA密钥计算函数(仅取账号右侧10位)
CCP函数修改版
Public Function CCP(Cle_CCP As String) As String ' 仅保留账号右侧10位数字 Dim processedAccount As String processedAccount = Right(Cle_CCP, 10) ' 补前导0至10位(保留原逻辑,基于处理后的账号) Dim padLength As Integer padLength = 10 - Len(processedAccount) Dim M As String M = "" For i = 1 To padLength M = M & "0" Next processedAccount = M & processedAccount ' 原计算逻辑不变 Dim M1 As Integer, M2 As Integer, M3 As Integer, M4 As Integer, M5 As Integer Dim M6 As Integer, M7 As Integer, M8 As Integer, M9 As Integer, M10 As Integer M1 = Mid(processedAccount, 1, 1) * 13 M2 = Mid(processedAccount, 2, 1) * 12 M3 = Mid(processedAccount, 3, 1) * 11 M4 = Mid(processedAccount, 4, 1) * 10 M5 = Mid(processedAccount, 5, 1) * 9 M6 = Mid(processedAccount, 6, 1) * 8 M7 = Mid(processedAccount, 7, 1) * 7 M8 = Mid(processedAccount, 8, 1) * 6 M9 = Mid(processedAccount, 9, 1) * 5 M10 = Mid(processedAccount, 10, 1) * 4 Dim total As Integer total = M1 + M2 + M3 + M4 + M5 + M6 + M7 + M8 + M9 + M10 ' 格式化结果为两位字符串 If total < 10 Then CCP = "0" & total Else CCP = CStr(total) End If CCP = Right(CCP, 2) End Function
RIP函数修改版
Public Function RIP(Cle_RIP As String) As String ' 仅保留账号右侧10位数字 Dim processedAccount As String processedAccount = Right(Cle_RIP, 10) ' 处理空值情况 If processedAccount = "" Then processedAccount = "0" End If ' 原计算逻辑不变 Dim temp As Long temp = CLng(processedAccount) * 100 temp = temp - 97 * Int(temp / 97) temp = temp + 85 If temp < 97 Then temp = temp + 97 End If temp = temp - 97 temp = 97 - temp ' 格式化结果为两位字符串 If temp < 10 Then RIP = "0" & temp Else RIP = CStr(temp) End If End Function
关键修改说明
- 两个函数都新增
processedAccount = Right(原账号参数, 10),确保仅用账号右侧10位计算:- 原账号长度≤10时,
Right返回全部账号内容,不影响短账号的原有计算逻辑 - 原账号长度>10时,自动截取最后10位作为计算基础
- 原账号长度≤10时,
- 保留原有的补位、计算、格式化逻辑,仅替换为处理后的账号字符串,保证计算规则不变
- 优化了变量命名,避免原函数中直接覆盖函数名的不规范写法,提升代码可读性
内容的提问来源于stack exchange,提问作者snollygoster
相关产品推荐
相关产品推荐

