切割行与数据并移动到不同的表VBA [英] Cutting Row with Data and moving to different sheet VBA

查看:296
本文介绍了切割行与数据并移动到不同的表VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

感谢任何帮助!

推荐答案

如果要剪切并粘贴,请参见下文:

If you want to cut and paste, see below:

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

Sheet1中单元格A1的上限值,并将其粘贴到Sheet2的单元格A1上。

现在,如果要将其并入代码中,请参见下文。 R>

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

上面的代码将G中的单元格中的所有行都全部删除,并将其粘贴到Sheet2中,从A1开始。

注意:我上面使用的Sheet2和Sheet1是表格代码。

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.

这篇关于切割行与数据并移动到不同的表VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆