The information in this article applies to:
SUMMARY
In the versions of Microsoft Visual Basic for Applications that ship with
Microsoft Excel versions 5.0 and later, FOR loops behave differently than
they do in the Microsoft Excel version 4.0 macro language.
Microsoft Excel Version 4.0 Macro Commands
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspIn Visual Basic, the number of iterations for the loop cannot be changed by changing the value of the variable used to set the ending value for the loop while the loop is in progress (this behavior is standard for most programming languages that use FOR loops). However, you can change the value of the variable in a Microsoft Excel version 4.0 style macro using the equivalent functions, FOR() and NEXT(). Microsoft Excel Version 4.0 Macro ExampleIn the following Excel 4.0 style macro, the loop is originally set to run ten times. However, it will only run 5 times because the ending value is modified during the loop. To test this behavior, enter the following on a Microsoft Excel 4.0 macro sheet:A1: y=10To run the macro, select cell A1, click Macro on the Tools menu, and then click Run. The ALERT() statement in A5 will display the value 6, which means that the loop only ran 5 times, as opposed to the 10 times that the original value of "y" was set for. Sample Visual Basic ProcedureIn the following Visual Basic macro, the loop is originally set to run 10 times and it will run 10 times, even though the macro changes the value of the variable that is used to set the ending value of the loop.To test this macro:
REFERENCES
"Visual Basic User's Guide," version 5.0, pages 143-147
For Additional query words: 5.00a 5.00c 7.00a XL98 XL97 XL7 XL5
Keywords : kbprg kbdta kbdtacode PgmLoop KbVBA |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |