Use of GOTO and RUN Macro Commands in Excel

Last reviewed: November 4, 1994
Article ID: Q70476
The information in this article applies to:
  • Microsoft Excel for Windows, versions 2.x, 3.0, 4.0, 5.0
  • Microsoft Excel for OS/2, versions 2.2, 3.0

SUMMARY

The GOTO macro function branches from the currently running macro and runs the sequence of macro commands at the line or defined name within the GOTO parameters and does not return. The RUN macro function proceeds to run the macro commands at the location specified within the RUN parameters, and when the =RETURN() line is encountered, returns to the macro line immediately following the RUN macro function and finishes running that macro.

MORE INFORMATION

Consider the following examples of the GOTO and RUN functions:

   A1:  =ALERT("this is the first set")
   A2:  =GOTO(A6)
   A3:  =ALERT("this is the first set again")
   A4:  =RETURN()
   A5:
   A6:  =ALERT("this is the second set")
   A7:  =RUN(A11)
   A8:  =ALERT("welcome back to the second set")
   A9:  =RETURN()
   A10:
   A11: =ALERT("welcome to this third step")
   A12: =RETURN()

When you run this macro at A1, Microsoft Excel generates an alert message showing "this is the first set." Then it branches to line A6 and displays the alert message "this is the second set." Microsoft Excel then jumps to line A11 and displays the alert message "welcome to this third set". When it encounters the =RETURN() command at A12, Microsoft Excel jumps back to A8 and displays the alert message "welcome back to the second set." When it executes =RETURN() at A9, the Microsoft Excel macro program stops and does not go back to A3 and continue.

The GOTO command is a branch command that breaks off running the rest of lines immediately following and executes a separate routine of macros. To return to a macro that broke off as a result of a GOTO command, you need to place another GOTO to return back. This extra step can be eliminated by using the RUN command.

Note: this is also true for version 5.0 macro sheets, but not modules.

REFERENCES

"Online Help," version 5.0 "Function Reference," version 4.0, pages 213-214, 367-368 "Function Reference," version 3.0, pages 118-119, 202-203 "Functions and Macros," version 2.1x, pages 303, 345


KBCategory: kbprg kbmacro
KBSubcategory:

Additional reference words: 2.0 2.00 2.01 2.1 2.10 2.2 2.20 2.21 3.0
3.00 4.0 4.00 5.0 5.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 4, 1994
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.