GoTo <Line Number> in VBA

From the VBA help file:

GoTo Statement

Briefly unconditionally attached to the specified line as part of the procedure.

Syntax

GoTo _line _

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

Notes

GoTo can only enter lines within the procedure in which it is displayed.

My question is: how can I go to line number using GoTo ? (I know how to go to the label.)

(Note: I ask this for the sake of curiosity. I do not intend to actually use GoTo in this way.)

+6
source share
5 answers

I understand your dislike for the answer “run the line with the line number”, but you cannot argue with the facts. This is exactly what they mean.

The VBA / VB6 syntax is intended for backward compatibility with the QuickBasic syntax, and before that with the GW-Basic / MS-Basic syntax, which dates from the end of 1970 and even earlier: the original Dartmouth BASIC language was created in the 60s.

In MS-Basic, as in any other basic implementation of the era, each line added to the program must begin with a line number. The line number told the main interpreter two things: a) that you store the line (otherwise the interpreter would execute it immediately) and b) at what position of the program the line belonged to. Why is something so secret? because when Basic was invented, it had to be interactive, in a world where the only form of interactivity was a command line prompt on a teletype-style print terminal.

And there were no shortcuts.

A typical base session might look like where > means the shell prompt (it is composed, but close enough to how it works). Remember: no cursor keys or screens. You type on a typewriter with a roll of paper instead of a screen - and the typewriter answers you by typing on paper !:

 Welcome to BASIC 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 should start somewhere.

At that time, you always used GOTO, specifying the line number where you want the code to hop. That was the way it worked. For instance:

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

QuickBasic was an extended version of Basic published by Microsoft that supported the optional compilation of programs into executable files, rather than interactively in the interpreter. Among other improvements, he also added these two features:

  • Since he worked full-screen with a full-featured graphical GUI editor, he does not need line numbers to indicate where each new line is; you just moved the cursor and typed: traditional line numbers were now optional . In fact, they were discouraged because in the full-featured editor they simply got in the way. But they could not simply remove them because they were so important for compatibility with BASIC, therefore they were still supported. And they are still there, even in VBA.

  • Since they did not want you to use line numbers, they needed an alternative to teams in which line numbers were targets, such as GOTO . You are now allowed to place text labels that can be used as targets for GOTO, etc.

So you can see that line numbers are not just “line labels made of numbers”. They are actually an alternative syntax that is supported for compatibility with older versions of the language.

What is it. The help file just tells you about the “modern” GOTO syntax (with text labels), and that if you really want to, you can still use the obsolete syntax with line numbers and the obsolete GOTO syntax, which was invented in the mid-1960s.

+19
source
 Sub Jump() 10 Dim A As Integer 20 A = 25 30 GoTo 50 40 A = 50 50 Debug.Print A End Sub 

This is a return to the old (really old) BASIC days where line numbers were needed. Now tags 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 ...

+8
source

One very useful goal for old-fashion line numbers is error handling. Many people use the standard Sort error handler:

name proc (args) by mistake goto handler code,, exit proc

Handler: debug.print err.number and "(" and err.description and ") in the module:" and ModuleName and "- Proc:" and ProcName at and now

Next Summary Exit Proc

That can be made somewhat more useful. The code is numbered for a line, since the ErrLine property will return the line number from the violating executable line.

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

+2
source

Declaring line numbers and declaring strokes is basically the same thing, but using line number as a big advantage: it doesn't use memory!

if your "ours from memory" you cannot declare but you can declare a line number and use it as "goTo"

sub mySub ()
....
by mistake goto 100
...
exit sub
100: msgbox ("Error")
end sub

0
source

I read a lot of comments that GOTO is bad programming.

Back in the old days, when the Hewlit Packard 25 had 25 memories for storing all instructions and variables, BASIC was still a novice. A whole symbolic instruction code and much better than doing in assembler 6502 I went to a computer conference where one of the experts was gathering about a new language that used blocks and None of GOTO GOSUB AND RETURN .. yes PASCAL At the time I I KNEW it was impossible to have such a language, but since I spent about 20 years of training and developing commercial software in PASCAL With MS supporting my office with VBA, of course, Pascal became rare even though Delphi was still used as a training language at the initial stage.

To shorten a short IT resource
If you consider the machine code built for IF, then finish it if the fact that VBA still evaluates ALL AND OR NOT XOR in If ....... Then when you need to evaluate one million times then GOTO can save you a few seconds.

However, on the topic ON Error Resume Next, many use them to check if a workbook is open or if a table exists on a closed workbook ...... etc. etc. In some cases, this is bad programming, because the program must check everything before showing the error, whereas for each .... until it is found, found = a = b only need to be checked until you find

My first training computer was a PDP 8 ... 8Kb .. Perforated teletype for printing and I / O. 7 Octal switches for loading. Then he moved on to Comodore 64 .. Currently, the 16 GB computer has 250,000 memory.

up-to-date commentary from the FBI CIA, etc .... Homeland Security Waddles them all.

0
source

Source: https://habr.com/ru/post/945213/


All Articles