gpt4 book ai didi

vba - 如何验证 LEI 代码

转载 作者:行者123 更新时间:2023-12-02 07:42:49 25 4
gpt4 key购买 nike

我正在编写一个宏来验证 LEI 代码。

LEI 代码为:

  1. 20 个字符的字符串
  2. 前 18 个字符字母数字
  3. 最后两个字符数字

示例:F50EOCWSQFAUVO9Q8Z97

验证

  1. 将字符串中的所有字母字符替换为 2 位数字以创建整数
  2. 如果整数除以 97 的模为 1,则代码有效,否则无效

我写了以下内容:

Sub Test()

Dim LEI_String As String

LEI_String = Range("B1")

LEI_String = Replace(LEI_String, "A", "10")
LEI_String = Replace(LEI_String, "B", "11")
LEI_String = Replace(LEI_String, "C", "12")
LEI_String = Replace(LEI_String, "D", "13")
LEI_String = Replace(LEI_String, "E", "14")
LEI_String = Replace(LEI_String, "F", "15")
LEI_String = Replace(LEI_String, "G", "16")
LEI_String = Replace(LEI_String, "H", "17")
LEI_String = Replace(LEI_String, "I", "18")
LEI_String = Replace(LEI_String, "J", "19")
LEI_String = Replace(LEI_String, "K", "20")
LEI_String = Replace(LEI_String, "L", "21")
LEI_String = Replace(LEI_String, "M", "22")
LEI_String = Replace(LEI_String, "N", "23")
LEI_String = Replace(LEI_String, "O", "24")
LEI_String = Replace(LEI_String, "P", "25")
LEI_String = Replace(LEI_String, "Q", "26")
LEI_String = Replace(LEI_String, "R", "27")
LEI_String = Replace(LEI_String, "S", "28")
LEI_String = Replace(LEI_String, "T", "29")
LEI_String = Replace(LEI_String, "U", "30")
LEI_String = Replace(LEI_String, "V", "31")
LEI_String = Replace(LEI_String, "W", "32")
LEI_String = Replace(LEI_String, "X", "33")
LEI_String = Replace(LEI_String, "Y", "34")
LEI_String = Replace(LEI_String, "Z", "35")


MsgBox Len(LEI_String)
Range("B2").Value = CCur(LEI_String) Mod 97
MsgBox CCur(LEI_String) Mod 97

End Sub

当然,我遇到了运行时错误 6 溢出错误,因为我正在使用的整数长度为 35 位。

有办法解决这个问题吗?

最佳答案

LEI 编号太大,不适合十进制类型。要获得模数,您必须在转换为基数 10 后计算每个数字的模数:

Private Sub Test()

Debug.Print IsValidLEI("F50EOCWSQFAUVO9Q8Z97") ' >> True '
Debug.Print IsValidLEI("T50EOCWSQFAUVO9Q8Z97") ' >> False '

End Sub

Public Function IsValidLEI(lei As String) As Boolean
Dim i As Long, c As Long, m As Long

For i = 1 To Len(lei) ' each character '
c = AscW(Mid(lei, i, 1)) ' get the character code (see ASCII table) '

Select Case c
Case 48 To 57 ' 0-9 -> 0-9 '
m = (m * 10 + c - 48) Mod 97 ' x10 to shift 1 digit, -48 to convert to base10 '
Case 65 To 90 ' A-Z -> 10-35 '
m = (m * 100 + c - 55) Mod 97 ' x100 to shift 2 digits, -55 to convert to base10 '
Case Else
Err.Raise 5, , "Unexpected character at " & i
End Select
Next

IsValidLEI = m = 1
End Function

关于vba - 如何验证 LEI 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com