VBA自定义函数在子例程中起作用,但在工作表中不起作用

问题描述 投票:0回答:1

我具有用于生成ean13条码的自定义VBA函数。该代码在Microsoft Excel 97-2013工作表中有效,但在上述版本中不起作用。我使用的是Microsoft Excel 2013,因此当我将此电子表格转换为Microsoft Excel 2013时,该功能将停止在工作表上运行。当我在子例程中对其进行测试时,它可以工作。

我已经从here复制了此代码

Public Function ean13$(chaine$)
  'V 1.0
  'Parameters: a 12-digit string
  'Return: * a chain which, displayed with the EAN13.TTF font, gives the barcode
  '         * an empty string if parameter supplied incorrect
  Dim i%, checksum%, first%, CodeBarre$, tableA As Boolean
  ean13$ = ""
  'Check that there are 12 characters
  If Len(chaine$) = 12 Then
    'And that these are many figures
    For i% = 1 To 12
      If Asc(Mid$(chaine$, i%, 1)) < 48 Or Asc(Mid$(chaine$, i%, 1)) > 57 Then
        i% = 0
        Exit For
      End If
    Next
    If i% = 13 Then
      'Calculation of the control key
      For i% = 2 To 12 Step 2
        checksum% = checksum% + Val(Mid$(chaine$, i%, 1))
      Next
      checksum% = checksum% * 3
      For i% = 1 To 11 Step 2
        checksum% = checksum% + Val(Mid$(chaine$, i%, 1))
      Next
      chaine$ = chaine$ & (10 - checksum% Mod 10) Mod 10
      'The first number is taken as is, the second comes from table A
      CodeBarre$ = Left$(chaine$, 1) & Chr$(65 + Val(Mid$(chaine$, 2, 1)))
      first% = Val(Left$(chaine$, 1))
      For i% = 3 To 7
        tableA = False
         Select Case i%
         Case 3
           Select Case first%
           Case 0 To 3
             tableA = True
           End Select
         Case 4
           Select Case first%
           Case 0, 4, 7, 8
             tableA = True
           End Select
         Case 5
           Select Case first%
           Case 0, 1, 4, 5, 9
             tableA = True
           End Select
         Case 6
           Select Case first%
           Case 0, 2, 5, 6, 7
             tableA = True
           End Select
         Case 7
           Select Case first%
           Case 0, 3, 6, 8, 9
             tableA = True
           End Select
         End Select
       If tableA Then
         CodeBarre$ = CodeBarre$ & Chr$(65 + Val(Mid$(chaine$, i%, 1)))
       Else
         CodeBarre$ = CodeBarre$ & Chr$(75 + Val(Mid$(chaine$, i%, 1)))
       End If
     Next
      CodeBarre$ = CodeBarre$ & "*"   'Adding central divider
      For i% = 8 To 13
        CodeBarre$ = CodeBarre$ & Chr$(97 + Val(Mid$(chaine$, i%, 1)))
      Next
      CodeBarre$ = CodeBarre$ & "+"   'Addition of end mark
      ean13$ = CodeBarre$
    End If
  End If
End Function

我正在工作表=ean13(123456789012)中使用此功能,但会引发错误#REF!但是当我在子例程中使用它时,它就可以工作

Function myBarcode()
MsgBox ean13("123456789012")
End Function
excel vba excel-2013
1个回答
0
投票

最后我弄清楚了,我只是将函数名ean13$替换为ean_13,所以它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.