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

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

问题描述

从VBA帮助文件中:

转到声明

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

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 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年代末甚至更早:原始的达特茅斯(Dartmouth) BASIC语言是在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实现一样,您添加到程序中的每一行都必须以行号开头.行号告诉基本解释器两件事:a)您正在存储该行(否则解释器将立即执行该行),以及b)该行所属的程序位置.为什么做这么神秘的事?因为当发明Basic的初衷是交互式的,所以在电传式的 printing 终端上,交互式的唯一形式是命令行提示符.

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天全站免登陆