VBA "Compile Error: Label not defined"

VBA "Compile Error: Label not defined"

我有一个 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

违规行是:

GoTo FunctionNotValidVarType

我在这段代码下面有函数FunctionNotValidVarType。我把它当作:

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

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

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

具体来说,GoTo FunctionNotValidVarType 将尝试执行以下行:

FunctionNotValidVarType:  'Do stuff here

您当前的代码中不存在。

如果你想调用另一个函数使用Call FunctionNotValidVarType

从对您的 Sub()

的通话中删除 Goto

如果你真的想使用 Goto(而你 shouldn't),你会

goto Label

Label:

其中标签由尾随冒号定义 :

删除单词 GoTo

GoTo 告诉代码跳转到一个标签,你想让它进入一个新的程序,而不是去一个标签

GoTo 过渡到标签,标签定义为 :

例如:

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

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

Call FunctionNotValidVarType
Exit Sub

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

GoTo 被认为不是一个好的做法,但如果您不关心,也可以看看 official docs.

中的 GoSub