Ciclos: For ... Next, Do ... Loop While, For Each ... Next

 Aprende con los siguientes ejemplos a utilizar los ciclos:

For ... Next Do ... Loop While For Each ... Next

Código 1:
-----------------------
Sub ciclo_For()
  Dim i As Long
  
  For i = 1 To Range("A" & Rows.Count).End(3).Row
    If Range("A" & i).Value = "SUMA" Then
      Rows(i).Copy Sheets("Hoja2").Range("A" & Rows.Count).End(3)(2)
    End If
  Next
End Sub
-----------------------
Código 2:
-----------------------
Sub Ciclo_While()
  Dim f As Range
  Dim celda As String
  
  Set f = Range("A:A").Find("SUMA", , xlValues, xlWhole)
  If Not f Is Nothing Then
    celda = f.Address
    Do
      Rows(f.Row).Copy Sheets("Hoja2").Range("A" & Rows.Count).End(3)(2)
      Set f = Range("A:A").FindNext(f)
    Loop While celda <> f.Address
  End If
End Sub
-----------------------

Código 3:
-----------------------
Sub Ciclo_For_Each()
Dim ar As Range
For Each ar In Range("A1", Range("A" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants).Areas
Rows(ar.Cells(ar.Rows.Count).Row).Copy Sheets("Hoja2").Range("A" & Rows.Count).End(3)(2)
Next
End Sub
-----------------------






No hay comentarios.:

Publicar un comentario