如果找到VBA,则搜索列并删除另一行

时间:2014-03-19 18:15:33

标签: excel vba excel-vba search

我有A栏,而我要做的就是搜索一个短语,说" test"然后如果找到这个短语,则在此之后删除2行。

如果在该行中找到该短语而不是如何删除另一行,我可以看到如何删除一行。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Public Sub DeleteRowsIfFound()
    Dim originCell As Range, numberOfRowsToDelete As Integer
    Dim blankCellLimit As Integer, numberOfBlankCells As Integer
    Dim label As String, index As Long, n As Integer

    Set originCell = Me.Range("A1")
    blankCellLimit = 5
    numberOfRowsToDelete = 2
    index = 0
    label = "test"

    Do
        If originCell.Offset(index, 0).Value = label Then
            For n = 0 To numberOfRowsToDelete - 1
                originCell.Offset(index + 1, 0).EntireRow.Delete
            Next
        ElseIf originCell.Offset(index, 0).Value = "" Then
            numberOfBlankCells = numberOfBlankCells + 1
        End If

        index = index + 1
    Loop While numberOfBlankCells < blankCellLimit
End Sub

这开始从单元格A1开始向下搜索列A,如果找到值为&#34的单元格,则测试&#34;然后它会删除它后面的下两行。