VBA“编译错误:标签未定义”

时间:2022-11-06 09:31:12

I have a VBA macro which gave me that error message.

我有一个VBA宏,它给了我错误信息。

Sub Function1()
    '   Give the user macro options based on how fast or slow the computer 
    '   is using advanced conditional compiling
    vuserChoice = MsgBox("This macro by default treats all numbers as decimals for maximum precision. If you are running this macro on an old computer, you may want to declare numbers as singles, to speed up the macro.")
    MsgBox ("Decimal: recommended for maximum precision. Also slower." & vbNewLine & "Long: not recommended. Rounds to nearest integer." & vbNewLine & "Single: not recommended. A lightweight double." & vbNewLine & "Integer: not recommended. Quick and low-precision.")

    If vuserChoice = "Decimal" Or "decimal" Then
        GoTo FunctionDecimal
    ElseIf vuserChoice = "Double" Or "double" Then
        GoTo FunctionDouble
    ElseIf vuserChoice = "Single" Or "single" Then
        GoTo FunctionSingle
    ElseIf vuserChoice = "Long" Or "long" Then
        GoTo FunctionLong
    Else
        GoTo FunctionNotValidVarType
    End If

    '   MEeff = measure of efflux due to crudely purified HDL in scintillation
    MsgBox "For additional information about this macro:" & vbNewLine & "1. Go to tab Developer" & vbNewLine & "2. Select Visual Basic or Macro." & vbNewLine & "See the comments or MsgBoxes (message boxes)."
End Sub

The offending line is:

违规行是:

GoTo FunctionNotValidVarType

I have the function FunctionNotValidVarType below this code. I have it as:

我在此代码下面有函数FunctionNotValidVarType。我有它:

Public Sub FunctionNotValidVarType()
    MsgBox "VarType " & VarType & " is not supported. Please check spelling."
End Sub

What do I need to do to let the first function recognize FunctionNotValidVarType? Thanks.

要让第一个函数识别FunctionNotValidVarType,我需要做什么?谢谢。

4 个解决方案

#1


5  

GoTo will try and transfer the code execution to a different position in the current Subroutine with the given label.

GoTo将尝试使用给定标签将代码执行转移到当前子例程中的不同位置。

Specifically, GoTo FunctionNotValidVarType will try and execute the line:

具体来说,GoTo FunctionNotValidVarType将尝试并执行该行:

FunctionNotValidVarType:  'Do stuff here

which doesn't exist in your current code.

在您当前的代码中不存在。

If you want to call another function use Call FunctionNotValidVarType

如果要调用另一个函数,请使用Call FunctionNotValidVarType

#2


2  

Remove the word GoTo

删除单词GoTo

GoTo tells the code to jump to a label, you want it to enter a new procedure, not go to a label

GoTo告诉代码跳转到标签,你希望它输入一个新程序,而不是去标签

#3


1  

Remove Goto from the call to your Sub()

从您的Sub()调用中删除Goto

If you really wanted to use a Goto (and you shouldn't), you would

如果你真的想使用Goto(你不应该),你会的

goto Label

Label:

where the label is defined by the trailing colon :

标签由尾部冒号定义:

#4


0  

GoTo transitions to a label, a label is defined with :

GoTo转换为标签,标签定义为:

For example:

Sub G()
On Error GoTo err_handling
  a=1/0
  Exit Sub
err_handling:
  MsgBox "Holy Shit, an error occurred !"
End Sub

To apply GoTo on a Sub you need call it and exit:

要在Sub上应用GoTo,您需要调用它并退出:

Call FunctionNotValidVarType
Exit Sub

(Technically, it is not the same as GoTo if you take the call stack into consideration, but the end result is the same)

(从技术上讲,如果考虑调用堆栈,它与GoTo不同,但最终结果是相同的)

GoTo is not considered a good practice, but if that doesn't concern you, take a look also at GoSub at the official docs.

GoTo不被认为是一种好习惯,但如果这与您无关,请在官方文档中查看GoSub。

#1


5  

GoTo will try and transfer the code execution to a different position in the current Subroutine with the given label.

GoTo将尝试使用给定标签将代码执行转移到当前子例程中的不同位置。

Specifically, GoTo FunctionNotValidVarType will try and execute the line:

具体来说,GoTo FunctionNotValidVarType将尝试并执行该行:

FunctionNotValidVarType:  'Do stuff here

which doesn't exist in your current code.

在您当前的代码中不存在。

If you want to call another function use Call FunctionNotValidVarType

如果要调用另一个函数,请使用Call FunctionNotValidVarType

#2


2  

Remove the word GoTo

删除单词GoTo

GoTo tells the code to jump to a label, you want it to enter a new procedure, not go to a label

GoTo告诉代码跳转到标签,你希望它输入一个新程序,而不是去标签

#3


1  

Remove Goto from the call to your Sub()

从您的Sub()调用中删除Goto

If you really wanted to use a Goto (and you shouldn't), you would

如果你真的想使用Goto(你不应该),你会的

goto Label

Label:

where the label is defined by the trailing colon :

标签由尾部冒号定义:

#4


0  

GoTo transitions to a label, a label is defined with :

GoTo转换为标签,标签定义为:

For example:

Sub G()
On Error GoTo err_handling
  a=1/0
  Exit Sub
err_handling:
  MsgBox "Holy Shit, an error occurred !"
End Sub

To apply GoTo on a Sub you need call it and exit:

要在Sub上应用GoTo,您需要调用它并退出:

Call FunctionNotValidVarType
Exit Sub

(Technically, it is not the same as GoTo if you take the call stack into consideration, but the end result is the same)

(从技术上讲,如果考虑调用堆栈,它与GoTo不同,但最终结果是相同的)

GoTo is not considered a good practice, but if that doesn't concern you, take a look also at GoSub at the official docs.

GoTo不被认为是一种好习惯,但如果这与您无关,请在官方文档中查看GoSub。