GoTo in VBA

后端 未结 5 2103
情书的邮戳
情书的邮戳 2021-02-16 00:10

From the VBA help file:

GoTo Statement

Branches unconditionally to a specified line within a procedure.

Syntax<

5条回答
  •  甜味超标
    2021-02-16 00:45

    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.

    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.

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

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

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

    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.

提交回复
热议问题