Home » excel » excel – Cutting Row with Data and moving to different sheet VBA

excel – Cutting Row with Data and moving to different sheet VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to cut a row that has the specified cell blank and then paste it into another sheet in the same workbook. My coding works fine to delete the row but everything I’ve tried to cut and paste keeps giving me errors. Here’s the working code that deletes the rows:

Sub Remove()

    'Remove No Denovo &/or No Peak Seq
    Dim n As Long
    Dim nLastRow As Long
    Dim nFirstRow As Long
    Dim lastRow As Integer

    ActiveSheet.UsedRange

    Set r = ActiveSheet.UsedRange
    nLastRow = r.rows.Count + r.Row - 1
    nFirstRow = r.Row

    For n = nLastRow To nFirstRow Step -1
        If Cells(n, "G") = "" Then Cells(n, "G").EntireRow.Delete
    Next n

End Sub

Thanks for any help!

How to&Answers:

If you want to cut and paste, see below:

With Sheet1
    .Range("A1").Cut Sheeet2.Range("A1")
End With

Above cut’s value in Cell A1 in Sheet1 and paste it on Cell A1 of Sheet2.
Now if you want to incorporate it in your code, see below.

Dim i As Long: i = 1
With Activesheet
    For n = nLastRow To nFirstRow Step -1
        If .Cells(n, "G") = "" Then
            .Cells(n, "G").EntireRow.Cut Sheet2.Cells(i, "A")
            .Cells(n, "G").EntireRow.Delete '~~> if you want to delete
            i = i + 1
        End If
    Next
End With

Above code cuts all rows with Cell in G blank and paste it in Sheet2 starting A1.
Note: Sheet2 and Sheet1 that I used above are sheet codenames.