Search code examples
excelvbacopy-pasteis-empty

Excel: Copy a row to another worksheet if a certain cell is not empty


i'm looking to use a marco that would be able to search a column in said sheet and if the cell is not empty (cell T), it copy that entire rows data/formatting and paste it into another sheet [sheet "genealogie" in my case] along with any other rows that contained a not empty cell.

https://i.sstatic.net/ISRcD.png

I tried this code but unfortunately it copies all the lines even when the cell in question (column T) is empty.

Sub Copyl()
Dim Cell As Range
With Sheets("Tableur")

    For Each Cell In .Range("B7:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        If Not IsEmpty(Range("T7:T" & .Cells(.Rows.Count, "T").End(xlUp).Row).Value) Then

            .Rows(Cell.Row).Copy Destination:=Sheets("genealogie").Rows(Cell.Row)

        End If
    Next Cell

End With
End Sub

Thanks by advance....


Solution

  • You check the entire range in the T column: just check the single cell in the corresponding row

    Sub Copyl()
        Dim Cell As Range
        With Sheets("Tableur")
    
        For Each Cell In .Range("B7:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        '--> check the value in T column in the corresponding row
            If Not IsEmpty(.Cells(Cell.Row, "T")) Then
    
                .Rows(Cell.Row).Copy Destination:=Sheets("genealogie").Rows(Cell.Row)
    
            End If
        Next Cell
        End With
    End Sub