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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24131874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:18:38  来源:igfitidea点击:

Cutting Row with Data and moving to different sheet VBA

excelvbaexcel-vba

提问by blaufer

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!

谢谢你的帮助!

回答by L42

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.

将 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

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.

上面的代码剪切所有带有 G 空白单元格的行,并将其粘贴到从 A1 开始的 Sheet2 中。
注意:我上面使用的 Sheet2 和 Sheet1 是工作表代号。