Inicio » VBA-Macros » VBA-Macros-Bucles » For Each… Next

For Each… Next

Instrucción que repite las instrucciones según el número de objetos especificados.

Por ejemplo, For each Cell de un rango en Excel.

Ejemplo

En este ejemplo vamos a construir un bucle que evalua cada celda de un rango. El rango será celdas A1:A5, que se
escribe como Range(Cells(1, 1), Cells(5, 1). Con el Exit For salimos del bucle al cumplir la condición.

Dim rngArea
 
rngArea = Range(Cells(1, 1), Cells(5, 1))
 
For Each Cell In rngArea
 If Cell = "Alexis" Then
  MsgBox "¡Encontró Alexis"
  Exit For
 End If
Next