The information in this article applies to:
SUMMARY
When you create macros or applications using Microsoft Visual Basic for
Applications, it is often necessary to run through a section of code
several times. Visual Basic for Applications provides several methods
with which to repeat, or "loop" through, a section of code.
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.aspWhen you are deciding which looping structure to use, there are several considerations to be aware of. For example: Do I know exactly how many times I want to loop through the code? Sample Visual Basic ProceduresThe following examples demonstrate the various looping structures available in Visual Basic for Applications. Unless otherwise indicated, the examples assume a cell or range of cells is selected. Some other things to note:
For...Each...NextThis example uses a For...Each...Next statement to loop through all the cells in a selected range (the comments show an alternative method):
For <variable> = <n1> to <n2>
Do...Until with Test at Beginning of LoopThis example uses a Do...Until loop to "walk" down a column of cells until the first empty cell is reached. Because the macro performs the test at the beginning of the loop, if the first cell is empty, the code inside the loop will not be run.
Do...Until with Test at End of LoopThis example also uses a Do loop, but it tests at the bottom of the loop. The first line of the macro tests the first cell. Without the first line of code to test the initial cell, the code would execute at least one time, because the loop tests at the bottom.
CAUTION: Do not branch into the body of a While...Wend loop without
executing the While statement. Doing so may cause run-time errors or
other problems that are difficult to locate.
The Do...Loop statement provides a more structured and flexible way to perform looping. The While...Wend loop is included in Visual Basic for Applications for backward compatibility. While...Wend LoopNOTE: The While...Wend loop is included in Visual Basic for Applications for backward compatibility.This example uses a While...Wend loop. This loop tests at the top of the loop only.
If...Then..GoTo LoopThis example creates a loop by using "If <condition> Then GoTo <line label>" structure. This structure is tested at the bottom of the loop.
REFERENCES
"Microsoft Excel Visual Basic User's Guide," version 5.0, pages 143-151
loopsand click Display. For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base: Q163435 VBA: Programming Resources for Visual Basic for Applications Additional query words: 8.00 xl97 VBA loop howto
Keywords : kbcode kbprg PgmHowto |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |