在 VBA 中转到 <行号>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16599982/
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
GoTo <Line number> in VBA
提问by TheIronKnuckle
From the VBA help file:
从 VBA 帮助文件:
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax
GoTo_line_
The required lineargument can be any line label or line number.
Remarks
GoTocan branch only to lines within the procedure where it appears.
转到语句
无条件分支到过程中的指定行。
句法
转到_行_
所需的行参数可以是任何行标签或行号。
评论
GoTo只能分支到它出现的过程中的行。
My question is, how can I jump to a line number using GoTo
? (I know how to jump to a label.)
我的问题是,如何使用 跳转到行号GoTo
?(我知道如何跳转到标签。)
(Note: I'm asking this for curiosity's sake. I have no intention of actually using GoTo
this way.)
(注意:我问这个是出于好奇。我无意实际使用GoTo
这种方式。)
回答by Euro Micelli
I understand your dislike of the answer "start the line with a line number", but you can't argue with facts. That is exactly what they mean.
我理解您不喜欢“以行号开始行”的答案,但您无法与事实争论。这正是他们的意思。
The syntax of VBA/VB6 is designed to be backwards-compatible with the syntax of QuickBasic, and before that with the syntax of GW-Basic/MS-Basic, which dates to the late 1970's and even earlier: the original Dartmouth BASIC Language was created in the '60s.
VBA/VB6 的语法被设计为与 QuickBasic 的语法向后兼容,在此之前与 GW-Basic/MS-Basic 的语法兼容,可追溯到 1970 年代后期甚至更早:原始的达特茅斯 BASIC 语言是创建于 60 年代。
In MS-Basic, like in every other Basic implementation of the era, every line you added to a program had to start with a line number. The line number told the Basic interpreter two things: a) that you were storing the line (otherwise the interpreter would execute it immediately), and b) in what position of the program the line belonged. Why do something so arcane? because when Basic was invented it was intended to be interactive, in a world where the only form of interactivity was a command-line prompt, on a teletype-style printingterminal.
在 MS-Basic 中,就像在那个时代的所有其他 Basic 实现中一样,您添加到程序中的每一行都必须以行号开头。行号告诉基本解释器两件事:a) 您正在存储该行(否则解释器会立即执行它),以及 b) 该行属于程序的哪个位置。为什么要做如此神秘的事情?因为当 Basic 被发明时,它的目的是交互式的,在一个唯一的交互形式是命令行提示的世界里,在电传式打印终端上。
And there were no labels.
而且没有标签。
A typical Basic session might have looked like this, where >
stands for a command processor prompt (this is made-up, but close enough to how it worked). Remember: there are no cursor keys or screens. You are typing on a typewriter - with a roll of paper instead of a screen - and the typewriter responds back at you by printing on the paper as well!:
典型的 Basic 会话可能如下所示,其中>
代表命令处理器提示符(这是虚构的,但与它的工作方式非常接近)。请记住:没有光标键或屏幕。你在打字机上打字——用一卷纸而不是屏幕——打字机也会通过在纸上打印来回应你!:
Welcome to B.A.S.I.C.
Ok <--- Ok told you the interpreter was ready
>LIST <--- print the program
Ok <--- No program, so nothing to list.
>PRINT 2 + 7 <--- No line number, so execute immediately
9 <--- The command executes
Ok
>30 PRINT 2 + 7 <--- Line number, so store the command in position 30
Ok
>10 I = 42 <--- Line number, so store in line 10
Ok
>20 PRINT I + 12 <--- Store on line 20, so insert between 10 and 30
Ok
>LIST <--- Print the program so far
10 I = 42
20 PRINT I + 12
30 PRINT 2 + 7
Ok
>RUN <--- Execute the stored program now
54 <--- line 10 has no output. Line 20 outputs this
9 <--- line 30 outputs this
Ok <--- Done running the program
>20 <--- an empty line number: it means delete the line
Ok
>LIST
10 I = 42
30 PRINT 2 + 7 <--- line 20 is gone!
Primitive? Maybe, but you have to start somewhere.
原始?也许吧,但你必须从某个地方开始。
Back then, you alwaysused GOTO by providing the line number where you wanted the code to jump. It was just how it worked. For example:
那时,您总是通过提供希望代码跳转的行号来使用 GOTO。这就是它的工作方式。例如:
10 PRINT "Testing, "
20 I = 1
30 PRINT I; ","
40 IF I >= 3 THEN 60
50 GOTO 30
60 END
QuickBasic was an enhanced version of Basic published by Microsoft that supported optionally compiling programs into executables, rather than running then in the interpreter interactively. Among other enhancements, it also added these two features:
QuickBasic 是 Microsoft 发布的 Basic 的增强版本,它支持有选择地将程序编译为可执行文件,而不是在解释器中以交互方式运行。除其他增强功能外,它还添加了以下两个功能:
Because it ran full-screen with a fully-featured GUI text editor, it didn't need line numbers to designate where each new line went; you just moved the cursor and typed: traditional line numbers were now optional. In fact, they were discouraged because in a full-featured editor, they just got in the way. But they couldn't just remove them because they were so central to BASIC compatibility, so they were still supported. And they still are, even in VBA.
Since they didn't want you to use line numbers, they needed an alternative for commands that required line numbers as targets, such as
GOTO
. you were now allowed to place line text labelsthat could be used as targets for GOTO, etc.
因为它使用功能齐全的 GUI 文本编辑器全屏运行,所以不需要行号来指定每一行的位置;您刚刚移动光标并输入:传统行号现在是可选的。事实上,他们很沮丧,因为在一个功能齐全的编辑器中,他们只是挡路了。但是他们不能仅仅删除它们,因为它们对于 BASIC 兼容性非常重要,所以它们仍然受到支持。即使在 VBA 中,它们仍然是。
由于他们不希望您使用行号,因此他们需要替代需要行号作为目标的命令,例如
GOTO
. 您现在可以放置可用作 GOTO 等目标的行文本标签。
So, you can see that line numbers are not just "line labels made out of digits". They are actually an alternative syntax that has been maintained for compatibility with older versions of the language.
因此,您可以看到行号不仅仅是“由数字组成的行标签”。它们实际上是为了与旧版本语言兼容而维护的替代语法。
That's it. The help file is simply telling you about the "modern" syntax of GOTO (with text labels), and that - if you reallywant to - you can still use the legacy syntax with line numbers and legacy GOTO syntax that was invented in the mid-1960's.
就是这样。帮助文件只是告诉您 GOTO 的“现代”语法(带有文本标签),并且 - 如果您真的想要 - 您仍然可以使用带有行号的旧语法和中期发明的旧 GOTO 语法-1960 年代。
回答by Tom Collins
Sub Jump()
10 Dim A As Integer
20 A = 25
30 GoTo 50
40 A = 50
50 Debug.Print A
End Sub
It's a throwback to the old (really old) BASIC days, where line numbers were required. Now labels are used.
这是对需要行号的旧(非常旧)BASIC 时代的回归。现在使用标签。
Sub Jump2()
Dim A As Integer
A = 25
GoTo JumpToHere
A = 50
JumpToHere:
Debug.Print A
End Sub
But using GoTo is considered poor programming, with the exception of OnError GoTo ...
但是使用 GoTo 被认为是糟糕的编程,除了 OnError GoTo ...
回答by user2953309
One very useful purpose for old fashion line numbers is for error handling. Many folks utilize a standard error handler of the Sort:
旧式行号的一个非常有用的目的是用于错误处理。许多人使用 Sort 的标准错误处理程序:
proc name (args) on error goto handler code . . exit proc
proc name (args) on error goto handler code 。. 退出过程
handler: debug.print err.number & "(" & err.description & ") in Module: " & ModuleName & "- Proc:" & ProcName at & now
handler: debug.print err.number & "(" & err.description & ") in Module: " & ModuleName & "- Proc:" & ProcName at & now
resume next
Exit Proc
恢复下一个
退出程序
Which can be made somewhat more useful In the code Is line numbered, as the ErrLine Property will return the line number of the offending executable line.
这可以在代码中变得更有用 是行编号,因为 ErrLine 属性将返回有问题的可执行行的行号。
debug.print err.number & "(" & err.description & ") - on Line " & errLine & " in Module: " & ModuleName & "- Proc:" & ProcName at & now
debug.print err.number & "(" & err.description & ") - 在线 " & errLine & " in Module: " & ModuleName & "- Proc:" & ProcName at & now
回答by user2904976
declaring the line number and declaring a lable is basicly the same
but using a line number as a big advantage: it does not use memory!
声明行号和声明标签基本相同,但使用行号作为一个很大的优势:它不占用内存!
if your "our of memory" you will not be able to declare a lable but you will be able to declare a line numer and use that as a "goTo"
如果您的“我们的记忆”,您将无法声明标签,但您将能够声明一个行号并将其用作“goTo”
sub mySub()
....
on error goto 100
...
exit sub
100:msgbox("Error")
end sub
sub mySub()
....
on error goto 100
...
exit sub
100:msgbox("Error")
end sub
回答by Harry S
I read many comments about GOTO being poor programming..
我阅读了很多关于 GOTO 编程不佳的评论。
Back in the olden days when a Hewlit Packard 25 had 25 memories to store all instructions and variables BASIC was still Beginners All purpose Symbolic Instruction Code and much better than doing things in 6502 Assembler I went to computer conference where the one of the experts was takling about a new language that used blocks and None of GOTO GOSUB AND RETURN .. yea PASCAL At the time I KNEW it was impossible to have such a language but since have spent about 20 years teaching and designing commercial software in PASCAL With MS backing their Office with VBA of course Pascal has become rare even though Delphi was still used as a early years teaching language.
回到过去,Hewlit Packard 25 有 25 个内存来存储所有指令和变量 BASIC 仍然是初学者 通用符号指令代码,比在 6502 汇编器中做事要好得多 我参加了一位专家正在讨论的计算机会议关于使用块的新语言,没有 GOTO GOSUB 和 RETURN ..使用 VBA 当然 Pascal 已经变得很少见了,尽管 Delphi 仍然被用作早期的教学语言。
To cut IT short
If you consider the machine code constructed for IF then else end if
and the fact that VBA still evaluates ALL of AND OR NOT XOR in an
If ....... Then when you need to evaluate one a few million times
the then GOTO can save you a few seconds.
缩短它
如果您考虑为 IF 构造的机器代码,则 else 结束 if 以及 VBA 仍然在 If 中评估所有 AND OR NOT XOR 的事实......那么当您需要评估几百万时次然后 GOTO 可以为您节省几秒钟。
However on the topic of ON Error Resume Next many use these to test if a workbook is open or a Sheets exists on a closed workbook... ...etc etc .. In some cases that is poor programming in that the program has to check all before showing an error whereas for each .... while Not Found found = a = b wend only has to check until found
然而,关于 ON Error Resume Next 的主题,许多人使用这些来测试工作簿是否打开或工作簿是否存在于关闭的工作簿上... ...等等等等.. 在某些情况下,编程很差,因为程序必须在显示错误之前检查所有,而对于每个 .... while Not Found found = a = b wend 只需要检查直到找到
My first teaching computer was a PDP 8 ... 8Kb .. Punched tape teletype for printing and I/O .. 7 Octal switches to boot. Then moved up to Comodore 64 .. Current 16 GB computer has memory of 250,000 of them.
我的第一台教学计算机是 PDP 8 ... 8Kb .. 用于打印和 I/O 的穿孔磁带电传打字机 .. 7 个八进制开关启动。然后升级到 Comodore 64 .. 当前 16 GB 计算机的内存为 250,000。
a topical comment with the FBI CIA etc .....Homeland Security Trumps them all.
联邦调查局中央情报局等的热门评论......国土安全部胜过他们所有人。