Loops

For simple 1-dimensional arrays, the For-Each tends to be faster than the For-Next.


For - Next

You can iterate through the elements of an array using a For - Next loop.

Public Sub ForNextLoop() 
Dim arValues As Variant
Dim oItem As Variant
Dim iArrayCount As Integer
   arValues = Array(1,2,3,4,5)

   For iArrayCount = 0 to UBound(arValues)
      oItem = arValues(iArrayCount)

   Next iArrayCount
End Sub

For - Each

You can iterate through the elements of an array using a For - Each loop.
The element must be declared with a Variant data type

Public Sub ForEachLoop() 
Dim arValues As Variant
Dim oItem As Variant
   arValues = Array(1,2,3,4,5)

   For Each oItem In arValues

   Next oItem
End Sub


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext