Saturday, 30 September 2017

FOR EACH LOOP


FOR EACH LOOP


for each loop
For Each element In Group
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
Next

Example 1

Private Sub Constant_demo_Click() 
   'fruits is an array
   fruits = Array("apple", "orange", "cherries")
   Dim fruitnames As Variant

   'iterating using For each loop.
   For Each Item In fruits
      fruitnames = fruitnames & Item & Chr(10)
   Next
  
   MsgBox fruitnames
End Sub

Example 2
For Each cl In ActiveSheet.Range("A1:A10")
 'Statements to be executed inside the loop 
Msgbox cl.value
Next cl



Example 3

‘if cell is greater then 0 cell will update as blank
For Each cl In ActiveSheet.Range("A1:C11") 
 If InStr(cl.Value, "a") > 0 Then 
  cl.Value = "" 
 End If 

Next cl

No comments:

Post a Comment

JOTHIDAM KARPOM VANGA