从一个工作表中复制行并粘贴到另一个工作表中基于单元格值的下一个可用行

时间:2018-11-13 15:07:30

标签: excel vba

我想检查一下代码中我做错了什么,基本上就像标题一样,我想从WS(ASN)中自动复制并粘贴到另一个工作表(存档)中,放入下一个可用的原始文件中,并删除其中一个(我从第一个ws(Archive)

复制了)

如果在“ K”列=“是”列中,则将原始内容从A-K列粘贴复制到工作表中的下一个可用原始文件(存档),并删除ws中的原始文件(ASN)

请查看代码并提供建议

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.EnableEvents = False

    Dim MatchRow As Long, FirstRow As Long, LastRow As Long
     Dim Destination As Range

    Dim ws As Worksheet
    Set ws = Sheets("ASN")

    FirstRow = 2
    LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row

     Dim i As Long
    i = FirstRow
    Do While i <= LastRow
        If ws.Range("K" & i).Value = "YES" Then
            MatchRow = ws.Range("K" & i).Row

             With Sheets("Archive")
                Set Destination = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
             End With

 ws.Range("A" & MatchRow & ":K" & MatchRow).Copy Destination

 ws.Rows(MatchRow).EntireRow.Delete

LastRow = LastRow - 1
        Else
            i = i + 1
            Application.EnableEvents = True
        End If
    Loop
End Sub

0 个答案:

没有答案