转到<行号>在 VBA 中 [英] GoTo <Line number> in VBA

查看:21
本文介绍了转到<行号>在 VBA 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 VBA 帮助文件:

From the VBA help file:

无条件地分支到过程中的指定行.

GoTo Statement

Branches unconditionally to a specified line within a procedure.

语法

转到 __

必需的 line 参数可以是任何行标签或行号.

The required line argument can be any line label or line number.

备注

GoTo 只能分支到过程中出现的行.

GoTo can 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.)

推荐答案

我理解您不喜欢以行号开始行"的答案,但您不能与事实争论.这正是他们的意思.

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.

VBA/VB6 的语法被设计为与 QuickBasic 的语法向后兼容,而在此之前与 GW-Basic/MS-Basic 的语法可以追溯到 1970 年代末甚至更早:原始的 DartmouthBASIC 语言创建于 60 年代.

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.

在 MS-Basic 中,与该时代的所有其他 Basic 实现一样,您添加到程序中的每一行都必须以 行号 开头.行号告诉 Basic 解释器两件事:a)您正在存储该行(否则解释器将立即执行它),以及 b)该行属于程序的哪个位置.为什么要做这么神秘的事情?因为当 Basic 被发明时,它的目的是交互,在一个交互的唯一形式是命令行提示的世界里,在一个电传打字风格的打印终端上.

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 printing terminal.

而且没有标签.

典型的 Basic 会话可能看起来像这样,其中 > 代表命令处理器提示符(这是虚构的,但与它的工作原理足够接近).请记住:没有光标键或屏幕.你在打字机上打字——用一卷纸而不是屏幕——打字机也会通过在纸上打印来回应你!:

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!:

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.

那时,您总是通过提供您希望代码跳转的行号来使用 GOTO.这就是它的工作方式.例如:

Back then, you always used GOTO by providing the line number where you wanted the code to jump. It was just how it worked. For example:

10 PRINT "Testing, "
20 I = 1
30 PRINT I; ","
40 IF I >= 3 THEN 60
50 GOTO 30
60 END

QuickBasic 是 Microsoft 发布的 Basic 的增强版本,支持选择性地将程序编译为可执行文件,而不是在解释器中交互运行.除其他增强功能外,它还添加了以下两个功能:

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:

  • 因为它使用功能齐全的 GUI 文本编辑器全屏运行,所以不需要行号来指定每个新行的位置;您只需移动光标并输入:传统的行号现在是可选的.事实上,他们很气馁,因为在功能齐全的编辑器中,他们只是碍事了.但是他们不能仅仅删除它们,因为它们对于 BASIC 兼容性非常重要,所以它们仍然受到支持.即使在 VBA 中,它们仍然是.

  • 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.

由于他们不希望您使用行号,因此他们需要替代需要行号作为目标的命令,例如 GOTO.您现在可以放置可用作 GOTO 等目标的行文本标签.

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 labels that could be used as targets for GOTO, etc.

因此,您可以看到行号不仅仅是由数字组成的行标签".它们实际上是一种替代语法,已被维护以与旧版本的语言兼容.

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.

就是这样.帮助文件只是告诉您 GOTO 的现代"语法(带有文本标签),并且 - 如果您真的想要 - 您仍然可以使用带有行号和传统 GOTO 的传统语法1960 年代中期发明的语法.

That's it. The help file is simply telling you about the "modern" syntax of GOTO (with text labels), and that - if you really want to - you can still use the legacy syntax with line numbers and legacy GOTO syntax that was invented in the mid-1960's.

这篇关于转到&lt;行号&gt;在 VBA 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆