On Error Goto Line Number Example
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting on error goto label ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join vba goto label the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes vba on error exit sub a minute: Sign up GoTo
Vba On Error Goto 0
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.) excel vba goto share|improve this question edited Apr 23 '15 at 18:57 niton 3,36051433 asked May 17 '13 at 1:18 TheIronKnuckle 2,83322137 4 I vba error handling best practices will not accept or upvote answers that try to get around things by labelling the individual lines with their line numbers well that's just how it's done! @TomCollins has your answer :) –Mat's Mug May 17 '13 at 3:05 2 Try the "what if..." approach to answering this question. Imagine if it did support just jumping to the Nth line. What would happen if you adding 1 line, even a blank line somewhere above it, or even accidentally added one? The entire code logic goes to pot. Gotos can be hard to follow code flow, let alone when they're going to non specific locations. –Deanna May 17 '13 at 7:45 Please stop tagging VBA questions as VB6. VB6 programmers really don't care about Excel macros. –Bob77 May 18 '13 at 17:54 add a comment| 4 Answers 4 active oldest votes up vote 17 down vote accepted 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
three flavors: compiler errors such as undeclared variables that prevent your code from compiling; user data entry error such as a user entering a negative value where only a positive number is acceptable; and run time errors, that occur when VBA cannot correctly http://www.cpearson.com/excel/errorhandling.htm execute a program statement. We will concern ourselves here only with run time errors. Typical run time errors include attempting to access a non-existent worksheet or workbook, or attempting to divide by zero. The example code in this article will use the division by zero error (Error 11) when we want to deliberately raise an error. Your application should make as many checks as possible during initialization to ensure that run time errors do not occur later. In Excel, this on error includes ensuring that required workbooks and worksheets are present and that required names are defined. The more checking you do before the real work of your application begins, the more stable your application will be. It is far better to detect potential error situations when your application starts up before data is change than to wait until later to encounter an error situation. If you have no error handling code and a run time error occurs, VBA will display on error goto its standard run time error dialog box. While this may be acceptable, even desirable, in a development environment, it is not acceptable to the end user in a production environment. The goal of well designed error handling code is to anticipate potential errors, and correct them at run time or to terminate code execution in a controlled, graceful method. Your goal should be to prevent unhandled errors from arising. A note on terminology: Throughout this article, the term procedure should be taken to mean a Sub, Function, or Property procedure, and the term exit statement should be taken to mean Exit Sub, Exit Function, or Exit Property. The term end statement should be taken to mean End Sub , End Function, End Property, or just End. The On Error Statement The heart of error handling in VBA is the On Error statement. This statement instructs VBA what to do when an run time error is encountered. The On Error statement takes three forms. On Error Goto 0 On Error Resume Next On Error Goto