删除行+根据列中的部分文字匹配向上移动

时间:2019-02-19 15:27:31

标签: vba if-statement delete-row

我希望宏删除在A列中包含 Blah的任何行并向上移动(以消除空白行)。

我一直在尝试,但是没有成功:

SomeObject

Sample Screenshot

1 个答案:

答案 0 :(得分:0)

这是使用自动筛选的示例,不需要循环。

Sub x()

Dim rData As Range

Application.ScreenUpdating = False

Range("A1").EntireRow.Insert
Range("A1:E1").Value = Array(1, 2, 3, 4,5) 'amend to how many columns you have

With ActiveSheet
    .AutoFilterMode = False
    .Range("A1").AutoFilter field:=1, Criteria1:="*General*"
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            Application.DisplayAlerts = False
            rData.Delete
            Application.DisplayAlerts = True
        End If
    End With
    .AutoFilterMode = False
    .Rows(1).Delete
End With

Application.ScreenUpdating = True

End Sub