GoTo in VBA

后端 未结 5 2115
情书的邮戳
情书的邮戳 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:26

    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"

    sub mySub()
    ....
    on error goto 100
    ...
    exit sub
    100: msgbox("Error")
    end sub

    0 讨论(0)
  • 2021-02-16 00:41

    One very useful purpose for old fashion line numbers is for error handling. Many folks utilize a standard error handler of the Sort:

    proc name (args) on error goto handler code . . exit proc

    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.

    debug.print err.number & "(" & err.description & ") - on Line " & errLine & " in Module: " & ModuleName & "- Proc:" & ProcName at & now

    0 讨论(0)
  • 2021-02-16 00:42
    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.

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

    0 讨论(0)
  • 2021-02-16 00:44

    I read many comments about GOTO being poor programming..

    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.

    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.

    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

    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.

    a topical comment with the FBI CIA etc .....Homeland Security Trumps them all.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题