Thursday, September 1, 2011

Excel Macro: Delete a row whose first cell begins with an input string


Following macro deletes a row whose first cell begins with an input value:

Option Explicit
Sub DeleteRows()
    'ask the user for an input string
    SrchStr = InputBox("Delete rows that begin with..")
    'add asterisk at the end for - begins with condition
    SrchStr = SrchStr + "*"
    Dim i As Long
    i = 2
    'loop till empty cell in first(1) column
    Do Until Trim(Cells(i, 1)) = ""
        If Cells(i, 1).Value Like SrchStr Then
            Cells(i, 1).EntireRow.Delete
            'no any increments cause this cell is being deleted
        Else
            i = i + 1
        End If
    Loop
End Sub