如果列A具有" xyz"则复制粘贴并删除整行。

时间:2014-10-06 06:44:49

标签: excel vba excel-vba copy copy-paste

我希望根据列A是否具有“xyz”来复制粘贴并删除整行 即:如果A1 =“xyz”,则将粘贴(a1:g1)复制到下一个空行并删除第1行:1。

1 个答案:

答案 0 :(得分:1)

xyz 置于其他所有位置之后的自定义排序可能是更好的解决方案。如果您需要复制/粘贴/删除,请记住最好从底部开始,并在删除行时在For ... Next循环中进行操作。

Dim r As Long, lr As Long
With ActiveSheet
    lr = .Cells(Rows.count, 1).End(xlUp).Row
    For r = lr To 1 Step -1
        If LCase(.Cells(r, 1)) = "abc" Then
            .Cells(r, 1).Resize(1, .Cells(r, Columns.count).End(xlToLeft).Column).Copy _
              Destination:=.Cells(Rows.count, 1).End(xlUp).Offset(1, 0)
            .Rows(r).Delete
        End If
    Next r
End With