1

我不知道这是否是提出这样一个问题的地方,但由于它与一些 VBA 脚本有关,我认为它可能会。

我一直在写这个宏函数:

Function Mod10(tl As String) As Byte

    Dim c(13) As Integer
    Dim er As Integer

    c(13) = Mid(tl, 14, 1) * 2
    c(12) = Mid(tl, 13, 1)
    c(11) = Mid(tl, 12, 1) * 2
    c(10) = Mid(tl, 11, 1)
    c(9) = Mid(tl, 10, 1) * 2
    c(8) = Mid(tl, 9, 1)
    c(7) = Mid(tl, 8, 1) * 2
    c(6) = Mid(tl, 7, 1)
    c(5) = Mid(tl, 6, 1) * 2
    c(4) = Mid(tl, 5, 1)
    c(3) = Mid(tl, 4, 1) * 2
    c(2) = Mid(tl, 3, 1)
    c(1) = Mid(tl, 2, 1) * 2
    c(0) = Mid(tl, 1, 1)

    For i = 0 To 13
        If c(i) > 9 Then
            c(i) = CInt(Left(c(i), 1)) + CInt(Right(c(i), 1))
        End If
    Next

    er = 0
    For i = 0 To 13
        er = er + c(i)
    Next

    Mod10 = 10 - er Mod 10

End Function

但正如您在这张图片中看到的那样:

在此处输入图像描述

它返回一个错误说#REF!。我在 fx 中写这个:=Mod10(a1)

该脚本可在此处找到:http: //kronsell.net/fikkontrol.htm(丹麦网站)它用于计算字符串的最后一位。有点像计算 EAN-13 代码时,只是另一种计算。

如果有 VBA 或 EXCEL 知识的人能告诉我该怎么做,我将不胜感激。

4

1 回答 1

5

不知道为什么,但它看起来像是Mod10一个保留关键字。我尝试将函数重命名为Mod_10并且它有效。

在此处输入图像描述

如果您持有Option Explicit模块顶部,则可能需要声明一些变量。

试试这个并从一个单元格中调用它=MOD_10(A1)

   Function MOD_10(tl As String) As Byte

    Dim i As Long
    Dim c(13) As Integer
    Dim er As Integer

    c(13) = Mid(tl, 14, 1) * 2
    c(12) = Mid(tl, 13, 1)
    c(11) = Mid(tl, 12, 1) * 2
    c(10) = Mid(tl, 11, 1)
    c(9) = Mid(tl, 10, 1) * 2
    c(8) = Mid(tl, 9, 1)
    c(7) = Mid(tl, 8, 1) * 2
    c(6) = Mid(tl, 7, 1)
    c(5) = Mid(tl, 6, 1) * 2
    c(4) = Mid(tl, 5, 1)
    c(3) = Mid(tl, 4, 1) * 2
    c(2) = Mid(tl, 3, 1)
    c(1) = Mid(tl, 2, 1) * 2
    c(0) = Mid(tl, 1, 1)

    For i = 0 To 13
        If c(i) > 9 Then
            c(i) = CInt(Left(c(i), 1)) + CInt(Right(c(i), 1))
        End If
    Next

    er = 0
    For i = 0 To 13
        er = er + c(i)
    Next

    MOD_10 = 10 - er Mod 10

End Function
于 2013-09-10T12:45:36.093 回答