使用数据切割行并移动到不同的表VBA

时间:2014-06-10 02:19:39

标签: excel vba excel-vba

我试图剪切指定单元格空白的行,然后将其粘贴到同一工作簿中的另一个工作表中。我的编码可以正常删除行,但我尝试剪切和粘贴的所有内容都会让我犯错误。这是删除行的工作代码:

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

如果要剪切和粘贴,请参阅以下内容:

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

Sheet1中单元格A1的上方剪切值,并将其粘贴到Sheet2的单元格A1上 现在,如果您想将其合并到代码中,请参阅下文。

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

上面的代码用G中的Cell填充所有行并将其粘贴到Sheet2中,从A1开始 注意:我上面使用的Sheet2和Sheet1是工作表代号。