将行粘贴到下一个空行

时间:2019-02-26 16:10:13

标签: excel vba

我正在尝试使用在状态列中填充“已部署”一词来剪切行,并将其粘贴到称为“已部署”的新工作表中。我需要将这些行粘贴到下一个空行中,这样就不会复制“已部署”工作表上的现有行。

每次marco运行时,我就开始重新格式化“已部署”工作表的代码,但被告知我需要保留所有数据并添加到其中,现在我陷入了困境。我在网上找不到任何方法。

这是我需要更改的代码,以复制到下一个可用的空行而不是第二行。请帮助!

Sub CutRows() 'cuts all rows with status deployed into the Deployed worksheet - working
    Dim cell As Range
    Dim lastRow As Long
    Dim i As Long
    Set Wksh1 = Worksheets("01 Feb 19")
    Set Wksh2 = Worksheets("Deployed")

    lastRow = Wksh1.Range("A" & Rows.Count).End(xlUp).Row 'finds the last row

    i = 2 ' copies onto wksh2 row 2

    For Each cell In Wksh1.Range("T1:T" & lastRow) 'looks in T column until the last row (loop)
        If cell.Value = "Deployed" Then 'searches for word deployed
            cell.EntireRow.Cut Wksh2.Cells(i, 1) 'cuts entire row into Deployed worksheet
            i = i + 1 'Added so that rows don't overwrite each other
        End If
    Next cell 'To close the cell loop (For loop)
End Sub

1 个答案:

答案 0 :(得分:0)

只需将i = 2替换为i = Wksh2.Range("A" & Wksh2.Rows.Count).End(xlUp).Row + 1