将行值复制到另一个工作表

时间:2018-06-02 18:00:01

标签: excel vba excel-vba

我现在似乎有精神障碍。我想要做的是让VBA复制一行,例如A2:H2(值只是必须的)一旦将特定值添加到列E,我的代码目前缺乏的功能就是复制值仅适用于行范围到另一个工作表。

不确定还能做什么。这是代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 5 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "worker" Then
            With Target.EntireRow
                .Copy Sheets("worker").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        ElseIf LCase(Target.Value) = "worker2" Then
            With Target.EntireRow
                .Copy Sheets("worker2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        ElseIf LCase(Target.Value) = "worker3" Then
            With Target.EntireRow
                .Copy Sheets("worker3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        End If
    End If
End Sub

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您可以使用偏移和调整大小来获取行中的范围

With Target
   .Offset(, -4).Resize(1, 8).Copy  
   Sheets("worker").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
   .EntireRow.Delete
End With

注意:

1)我们现在正在使用Target而不是Target.EntireRow

2).Offset(, -4).Resize(1, 8)是" A:H"列E是目标列