Search notes:

Excel: delete rows meeting an arbitrary criteria

The following function takes two arguments: a range and a formula.
The formula is expected to contain a @.
The function then iterates over each cell in the range and applies the function to the cell. During the iteration, it replaces the @ with the actual value of the cell.
Rows in which the formula is true, will be deleted.
option explicit

sub deleteRowsWithCriteria(searchRange as range, formula as string) ' {

    dim cell       as range
    dim foundCells as range

    for each cell in searchRange ' {

        dim cellValue as string

        if typeName(cell.value) = "String" then
           cellValue = """" & cell.value & """"
        else
           cellValue =        cell.value
        end if

        dim formula_ as string
        formula_ = replace(formula, "@", cellValue)
    '   debug.print(formula_)

        if application.evaluate(formula_) then
           if foundCells is nothing then
              set foundCells = cell
           else
              set foundCells = application.union(foundCells, cell)
           end if
        end if

    next cell ' }

    if not foundCells is nothing then
       foundCells.entireRow.delete
    end if

end sub ' }
Github repository about-Excel, path: /helpers/delRowsCriteria.bas
When using the function, application.inputBox() might be used to select a range.
The following example deletes rows in which the selected cells have the letter a in the second place.
call deleteRowsWithCriteria(application.inputBox("", type := 8), "mid(@, 2, 1) = ""a""")
This call deletes rows where values of the cells are larger than 2.
call deleteRowsWithCriteria(application.inputBox("", type := 8), "@>2")

See also

Excel helpers

Index