VBA"编译错误:标签未定义"

时间:2015-06-24 15:05:01

标签: excel vba excel-vba goto

我有一个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需要做什么?感谢。

4 个答案:

答案 0 :(得分:5)

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

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

FunctionNotValidVarType:  'Do stuff here

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

如果您想使用Call FunctionNotValidVarType

调用其他功能

答案 1 :(得分:2)

删除单词GoTo

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

答案 2 :(得分:1)

从通话Goto

中删除Sub()

如果您真的想使用Goto(以及 shouldn't ),那么

goto Label

Label:

其中标签由尾部冒号:

定义

答案 3 :(得分:0)

GoTo转换为标签,标签使用:

定义

例如:

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

要在GoTo上应用Sub,您需要致电并退出:

Call FunctionNotValidVarType
Exit Sub

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

GoTo不被视为良好做法,但如果这与您无关,请查看GoSub处的javac

相关问题