VBA“编译错误:标签未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31030162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA "Compile Error: Label not defined"
提问by Thomas Shera
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 FunctionNotValidVarTypebelow 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?谢谢。
回答by kaybee99
GoTowill try and transfer the code execution to a different position in the current Subroutine with the given label.
GoTo将尝试将代码执行转移到当前子程序中具有给定标签的不同位置。
Specifically, GoTo FunctionNotValidVarTypewill 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
回答by Sam
Remove the word GoTo
删除这个词 GoTo
GoTotells the code to jump to a label, you want it to enter a new procedure, not go to a label
GoTo告诉代码跳转到一个标签,你想让它进入一个新的程序,而不是去一个标签
回答by FreeMan
回答by Uri Goren
GoTotransitions 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 GoToon a Subyou need call it and exit:
要申请GoToaSub你需要调用它并退出:
Call FunctionNotValidVarType
Exit Sub
(Technically, it is not the same as GoToif you take the call stack into consideration, but the end result is the same)
(从技术上讲,这与GoTo考虑调用堆栈不同,但最终结果是相同的)
GoTois not considered a good practice, but if that doesn't concern you, take a look also at GoSubat the official docs.
GoTo不被认为是一个很好的做法,但如果不关注你,看看在同样GoSub的官方文档。

