我有一个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
需要做什么?感谢。
答案 0 :(得分:5)
GoTo
将尝试将代码执行转移到当前子例程中具有给定标签的不同位置。
具体来说,GoTo FunctionNotValidVarType
会尝试执行以下行:
FunctionNotValidVarType: 'Do stuff here
在您当前的代码中不存在。
如果您想使用Call FunctionNotValidVarType
答案 1 :(得分:2)
删除单词GoTo
GoTo
告诉代码跳转到标签,您希望它输入新程序,而不是转到标签
答案 2 :(得分:1)
答案 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
。