XL: FOR Behaves Differently in Visual Basic than in XLM MacroLast reviewed: February 3, 1998Article ID: Q111725 |
The information in this article applies to:
SUMMARYIn 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. The respective macro commands are as follows:
Visual Basic ------------ For counter = startValue To endValue Next counter Microsoft Excel Version 4.0 Macro Commands ------------------------------------------ =FOR("counter",startValue,endValue) =NEXT() 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 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 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/refguide/default.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=10 A2: =FOR("x",1,y) A3: y=5 A4: =NEXT() A5: =ALERT(x) A6: =RETURN()To 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 more information about FOR, choose Contents from the Help menu, select Programming With Visual Basic, and then choose the Search button in Help and type the following:
For |
Additional query words: 5.00 5.00a 5.00c 7.00 7.00a XL98 XL97 XL7 XL5
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |