For each item in fruits fruitnames fruitnames item

This preview shows page 51 - 57 out of 144 pages.

For Each Item In fruits fruitnames = fruitnames & Item & Chr(10) Next MsgBox fruitnames End Sub When the above code is executed, it prints all the fruit names with one item in each line. apple orange cherries
VBA 45 While Wend Loops In a While Wend loop, if the condition is True, all the statements are executed until the Wend keyword is encountered. If the condition is false, the loop is exited and the control jumps to the very next statement after the Wend keyword. Syntax Following is the syntax of a While..Wend loop in VBA. While condition(s) [statements 1] [statements 2] ... [statements n] Wend Flow Diagram
VBA 46 Example Private Sub Constant_demo_Click() Dim Counter : Counter = 10 While Counter < 15 ' Test value of Counter. Counter = Counter + 1 ' Increment Counter. msgbox "The Current Value of the Counter is : " & Counter Wend ' While loop exits if Counter Value becomes 15. End Sub When the above code is executed, it prints the following in a message box. The Current Value of the Counter is : 11 The Current Value of the Counter is : 12 The Current Value of the Counter is : 13 The Current Value of the Counter is : 14 The Current Value of the Counter is : 15 Do While Loops A Do While loop is used when we want to repeat a set of statements as long as the condition is true. The condition may be checked at the beginning of the loop or at the end of the loop. Syntax Following is the syntax of a Do While loop in VBA. Do While condition [statement 1] [statement 2] ... [statement n] [Exit Do]
VBA 47 [statement 1] [statement 2] ... [statement n] Loop Flow Diagram Example The following example uses Do while loop to check the condition at the beginning of the loop. The statements inside the loop are executed, only if the condition becomes True. Private Sub Constant_demo_Click() Do While i < 5 i = i + 1 msgbox "The value of i is : " & i Loop End Sub
VBA 48 When the above code is executed, it prints the following output in a message box. The value of i is : 1 The value of i is : 2 The value of i is : 3 The value of i is : 4 The value of i is : 5 Alternate Syntax There is also an alternate Syntax for Do while loop which checks the condition at the end of the loop. The major difference between these two syntax is explained in the following example. Do [statement 1] [statement 2] ... [statement n] [Exit Do] [statement 1] [statement 2] ... [statement n] Loop While condition Example The following example uses Do while loop to check the condition at the end of the loop. The Statements inside the loop are executed at least once, even if the condition is False. Private Sub Constant_demo_Click() i = 10 Do i = i + 1
VBA 49 MsgBox "The value of i is : " & i Loop While i < 3 'Condition is false.Hence loop is executed once. End Sub When the above code is executed, it prints the following output in a message box. The value of i is : 11 Do Until Loops A Do Until loop is used when we want to repeat a set of statements as long as the condition is false. The condition may be checked at the beginning of the loop or at the end of loop.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture