Excel VBA自定义函数在子例程中工作,但在工作表中不工作
我有一个自定义VBA函数,用于生成ean13条形码。此代码在Microsoft Excel 97-2013工作表中有效,但在上述版本中无效。我使用的是Microsoft Excel 2013,因此当我将此电子表格转换为Microsoft Excel 2013时,该功能将停止工作表。当我在子程序中测试它时,它正在工作 我已从中复制了此代码Excel VBA自定义函数在子例程中工作,但在工作表中不工作,excel,vba,excel-2013,Excel,Vba,Excel 2013,我有一个自定义VBA函数,用于生成ean13条形码。此代码在Microsoft Excel 97-2013工作表中有效,但在上述版本中无效。我使用的是Microsoft Excel 2013,因此当我将此电子表格转换为Microsoft Excel 2013时,该功能将停止工作表。当我在子程序中测试它时,它正在工作 我已从中复制了此代码 最后我找到了答案,我只是将函数名ean13$替换为ean_13,这样它就可以工作了 Public Function ean13$(chaine$) 'V 1
最后我找到了答案,我只是将函数名
ean13$
替换为ean_13
,这样它就可以工作了
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
Function myBarcode()
MsgBox ean13("123456789012")
End Function