XL: FOR Behaves Differently in Visual Basic than in XLM Macro

ID: Q111725

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.

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 INFORMATION

Microsoft 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/refguide/

In 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 Example

In 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 Procedure

In 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:

1. Enter the following in a new Visual Basic module:

      Sub MyLoop()
          Dim x, y As Integer

          y = 10
          For x = 1 To y
              y = 5
          Next x
          MsgBox x
      End Sub

2. Position the insertion point in the line that reads "Sub MyLoop()" and
   either press F5 or click Start on the Run menu.

The MsgBox statement in the above macro displays the value 11, which means the loop ran through 10 times, even though we changed the value of the variable used to set the ending value of the loop.

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
Keywords          : kbprg kbdta kbdtacode PgmLoop KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,7.0a,97; MACINTOSH:5.0,5.0a,98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999