Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoTo <Line number> in VBA

Tags:

excel

vba

goto

vb6

From the VBA help file:

GoTo Statement

Branches unconditionally to a specified line within a procedure.

Syntax

GoTo _line_

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

Remarks

GoTo can branch only to lines within the procedure where it appears.

My question is, how can I jump to a line number using GoTo? (I know how to jump to a label.)

(Note: I'm asking this for curiosity's sake. I have no intention of actually using GoTo this way.)

like image 509
TheIronKnuckle Avatar asked May 17 '13 01:05

TheIronKnuckle


2 Answers

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.

like image 100
Euro Micelli Avatar answered Oct 17 '22 06:10

Euro Micelli


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

like image 41
Tom Collins Avatar answered Oct 17 '22 06:10

Tom Collins