Microsoft KB Archive/109780: Difference between revisions
(importing KB archive) |
m (Text replacement - """ to """) |
||
Line 68: | Line 68: | ||
The following Visual Basic procedure demonstrates the difference between using the GoTo statement and the Call statement. | The following Visual Basic procedure demonstrates the difference between using the GoTo statement and the Call statement. | ||
<pre class="codesample"> Sub One() | <pre class="codesample"> Sub One() | ||
MsgBox ( | MsgBox ("One()") ' Displays message box. | ||
' Sends control to Line10 below. | ' Sends control to Line10 below. | ||
' The GoTo Line10 statement directs the program to branch to the | ' The GoTo Line10 statement directs the program to branch to the | ||
' line labeled | ' line labeled "Line10:", the message box "unexecuted code" is | ||
' skipped. | ' skipped. | ||
GoTo Line10 | GoTo Line10 | ||
' This message box is not displayed. | ' This message box is not displayed. | ||
MsgBox ( | MsgBox ("unexecuted code") | ||
</pre> | </pre> | ||
Line10: | Line10: | ||
<pre class="codesample"> ' Displays message box. | <pre class="codesample"> ' Displays message box. | ||
MsgBox ( | MsgBox ("Line10 of One()") | ||
' The | ' The "Call Two()" line executes the subroutine defined as "Two()", | ||
' sends a message box indicating that the subprocedure is executing, | ' sends a message box indicating that the subprocedure is executing, | ||
' then returns to Sub One executing the line immediately after the | ' then returns to Sub One executing the line immediately after the | ||
Line 93: | Line 93: | ||
' Message box displayed. | ' Message box displayed. | ||
MsgBox ( | MsgBox ("back to One(); returning from Two()") | ||
End Sub | End Sub | ||
Line 99: | Line 99: | ||
Sub Two() | Sub Two() | ||
' Message box displayed. | ' Message box displayed. | ||
MsgBox ( | MsgBox ("Two()") | ||
End Sub | End Sub | ||
</pre> | </pre> | ||
Line 107: | Line 107: | ||
The following Visual Basic code makes a call to a another Sub procedure without using the Call statement. | The following Visual Basic code makes a call to a another Sub procedure without using the Call statement. | ||
<pre class="codesample"> Sub One() | <pre class="codesample"> Sub One() | ||
MsgBox ( | MsgBox ("One()") ' Message box displayed. | ||
two ' Call made to Sub named two(). | two ' Call made to Sub named two(). | ||
MsgBox ( | MsgBox ("return from Two()") ' Message box displayed. | ||
End Sub | End Sub | ||
Sub Two() | Sub Two() | ||
MsgBox ( | MsgBox ("Two()") ' Message box displayed. | ||
End Sub | End Sub | ||
</pre> | </pre> | ||
Line 124: | Line 124: | ||
' User input requested. Val() to turn input text into a number. | ' User input requested. Val() to turn input text into a number. | ||
number = Val(InputBox( | number = Val(InputBox("Enter a 1 or a 2.")) | ||
' Condition that is evaluated based on user input. | ' Condition that is evaluated based on user input. | ||
Line 134: | Line 134: | ||
</pre> | </pre> | ||
Line1: | Line1: | ||
<pre class="codesample"> MsgBox ( | <pre class="codesample"> MsgBox ("Great! You entered a " &amp; number &amp; ".") | ||
GoTo LastLine | GoTo LastLine | ||
</pre> | </pre> | ||
Line2: | Line2: | ||
<pre class="codesample"> MsgBox ( | <pre class="codesample"> MsgBox ("Sorry, you must enter a 1 or a 2.") | ||
</pre> | </pre> | ||
LastLine: | LastLine: | ||
<pre class="codesample"> MsgBox ( | <pre class="codesample"> MsgBox ("End of program.") | ||
End Sub | End Sub | ||
</pre> | </pre> |
Revision as of 10:49, 20 July 2020
Article ID: 109780
Article Last Modified on 6/11/2007
APPLIES TO
- Microsoft Excel 2000 Standard Edition
- Microsoft Excel 97 Standard Edition
- Microsoft Excel 98 for Macintosh
This article was previously published under Q109780
SUMMARY
In Microsoft Excel versions 5.0 and later, you can use the GoTo and Call statements to branch to other sections of a Microsoft Visual Basic for Applications procedure.
NOTE: In earlier versions of Microsoft Excel, you can do this with the GOTO() and RUN() functions.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
GoTo
The GoTo statement jumps to and executes the instructions at the specific line label and continues executing until the end of the program is reached.
Call
The Call statement runs a procedure and then returns to the line immediately after the Call statement in the originating procedure. The Call statement can also pass arguments of any declared data type to the called procedure and can transfer control to a Sub procedure, Function procedure, dynamic link library (DLL) procedure, or Macintosh code resource procedure.
The following examples demonstrate how to use GoTo and Call.
Example 1: The Difference Between Call and GoTo
The following Visual Basic procedure demonstrates the difference between using the GoTo statement and the Call statement.
Sub One() MsgBox ("One()") ' Displays message box. ' Sends control to Line10 below. ' The GoTo Line10 statement directs the program to branch to the ' line labeled "Line10:", the message box "unexecuted code" is ' skipped. GoTo Line10 ' This message box is not displayed. MsgBox ("unexecuted code")
Line10:
' Displays message box. MsgBox ("Line10 of One()") ' The "Call Two()" line executes the subroutine defined as "Two()", ' sends a message box indicating that the subprocedure is executing, ' then returns to Sub One executing the line immediately after the ' Call statement. ' Call made to Sub procedure named two() Call Two ' Message box displayed. MsgBox ("back to One(); returning from Two()") End Sub Sub Two() ' Message box displayed. MsgBox ("Two()") End Sub
NOTE: The sample code above does not pass any arguments.
Example 2: An Alternative to the Call Statement
The following Visual Basic code makes a call to a another Sub procedure without using the Call statement.
Sub One() MsgBox ("One()") ' Message box displayed. two ' Call made to Sub named two(). MsgBox ("return from Two()") ' Message box displayed. End Sub Sub Two() MsgBox ("Two()") ' Message box displayed. End Sub
Example 3: Using GoTo for Conditional Branching
The following code shows how you can combine the If...Then...Else statement with the GoTo statement to provide more branching options or to create the ability to return to a specific location after the GoTo statement is executed.
Sub GetInput() Dim number as Integer ' Used for input variable. ' User input requested. Val() to turn input text into a number. number = Val(InputBox("Enter a 1 or a 2.")) ' Condition that is evaluated based on user input. If number = 1 Or number = 2 Then GoTo Line1 Else GoTo Line2 End If
Line1:
MsgBox ("Great! You entered a " & number & ".") GoTo LastLine
Line2:
MsgBox ("Sorry, you must enter a 1 or a 2.")
LastLine:
MsgBox ("End of program.") End Sub
Additional query words: 5.00a 5.00c 7.00a XL98 XL97 XL7 XL5 XL
Keywords: kbdtacode kbhowto kbprogramming KB109780