While Wend Loop

The While...Wend statement is functionally equivalent to the Do...While loop. This statement is a carryover from earlier versions of Microsoft Basic and is included in VBA for backward compatibility. The loop begins with the keyword While and ends with the keyword Wend. Here is the syntax:

While condition statementl statement2 statements

Wend

The condition is tested at the top of the loop. The statements are executed as long as the given condition is true. Once the condition is false, Visual Basic exits the loop. Let's see an example of a procedure that uses the While.Wend looping structure.

1. Insert a new module into the current project. Rename the module WhileLoop.

2. Enter the following procedure in the WhileLoop module:

Sub ChangeRHeight()

While ActiveCell <>""

ActiveCell.RowHeight = 28 ActiveCell.Offset(1, 0).Select

Wend End Sub

3. Enter some data in cells B1:B4.

4. Select cell B1 and run the ChangeRHeight procedure.

The ChangeRHeight procedure shown above sets the row height to 28 when the active cell is not empty.

0 0

Post a comment