IF语句将数据移动到另一个工作表但只有一定范围

时间:2017-10-26 10:28:08

标签: excel vba excel-vba

请问您如何调整以下代码以仅移动A列的行数据?

Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Column = 11 Then
  If Target = "Complete" Then
   nxtRw = Sheets("Completed").Range("A" & Rows.Count).End(xlUp).Row + 1
    Target.EntireRow.Copy Sheets("Completed").Range("A" & nxtRw)
     Application.EnableEvents = False
      Target.EntireRow.Delete shift:=xlUp
     Application.EnableEvents = True
     ElseIf Target.Column = 11 Then
  If Target = "Cancelled" Then
   nxtRw = Sheets("Completed").Range("A" & Rows.Count).End(xlUp).Row + 1
    Target.EntireRow.Copy Sheets("Completed").Range("A" & nxtRw)
     Application.EnableEvents = False
      Target.EntireRow.Delete shift:=xlUp
     Application.EnableEvents = True
  End If
 End If
 End If
End Sub

此刻它将整行移动过来。在它移动到的工作表中,我在行I,J,K中有其他验证列表,它在复制时删除。

非常感谢任何帮助

感谢

马特

1 个答案:

答案 0 :(得分:0)

使用“调整大小”属性。你也可以稍微缩短你的If。

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 11 Then
    If Target = "Complete" Then
        nxtRw = Sheets("Completed").Range("A" & Rows.Count).End(xlUp).Row + 1
        Cells(Target.Row, 1).Resize(, 8).Copy Sheets("Completed").Range("A" & nxtRw)
        Application.EnableEvents = False
        Target.EntireRow.Delete shift:=xlUp
        Application.EnableEvents = True
    ElseIf Target = "Cancelled" Then
        nxtRw = Sheets("Completed").Range("A" & Rows.Count).End(xlUp).Row + 1
        Cells(Target.Row, 1).Resize(, 8).Copy Sheets("Completed").Range("A" & nxtRw)
        Application.EnableEvents = False
        Target.EntireRow.Delete shift:=xlUp
        Application.EnableEvents = True
    End If
End If

End Sub