VBA macros · Data

VBA macro to delete rows based on a cell value in Excel

Delete every row whose value in a chosen column matches a word such as Cancelled. Fast Union-based deletion, case-insensitive matching, easy to adapt.

Windows + Mac

Removing every Cancelled order, every zero-quantity line or every row for a closed branch is one of the most common clean-up jobs in Excel. Filtering and deleting by hand works, but it is easy to delete the wrong rows when the filter is left on by mistake. This macro asks for the value, checks one column and deletes all matching rows in one step.

VBA
Option Explicit

Sub DeleteRowsBasedOnValue()
    Const KEY_COL As String = "C"           ' column to check
    Const FIRST_ROW As Long = 2             ' row 1 holds the headings
    Dim ws As Worksheet
    Dim target As Variant
    Dim lastRow As Long, r As Long
    Dim toDelete As Range
    Dim n As Long

    Set ws = ActiveSheet
    target = Application.InputBox("Delete rows where column " & KEY_COL & " equals:", _
                                  "Delete rows", "Cancelled", Type:=2)
    If VarType(target) = vbBoolean Then Exit Sub      ' Cancel

    lastRow = ws.Cells(ws.Rows.Count, KEY_COL).End(xlUp).Row
    Application.ScreenUpdating = False

    For r = FIRST_ROW To lastRow
        ' .Text compares what the cell shows; StrComp ignores case
        If StrComp(Trim$(ws.Cells(r, KEY_COL).Text), Trim$(target), vbTextCompare) = 0 Then
            If toDelete Is Nothing Then
                Set toDelete = ws.Rows(r)
            Else
                Set toDelete = Union(toDelete, ws.Rows(r))
            End If
            n = n + 1
        End If
    Next r

    If Not toDelete Is Nothing Then toDelete.Delete
    Application.ScreenUpdating = True

    MsgBox n & " row(s) deleted.", vbInformation
End Sub
Delete rows where a column equals a given value

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick DeleteRowsBasedOnValue and click Run.
  5. Change KEY_COL to the column you want to check before running, for example "E".
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

How it works

Two constants at the top control where the macro looks: KEY_COL is the column letter and FIRST_ROW skips the heading row so it can never be deleted. The value is asked for with Application.InputBox, which returns False on Cancel; checking VarType for a Boolean is the reliable way to detect that (see Talking to the user: MsgBox and InputBox).

Each cell is compared through .Text, the value as it is displayed. That makes numbers and dates easy: type 0 to match cells that show 0, or 2026-03-01 to match a date shown in that format. Trim$ removes stray spaces on both sides and StrComp(..., vbTextCompare) ignores upper and lower case, so cancelled, Cancelled and CANCELLED all match.

Matching rows are collected into one range with Union and deleted together after the loop. One deletion is far faster than many, and the row numbers never shift during the loop. The same technique is used in VBA macro to delete blank rows in Excel.

Change it

To delete rows that contain the word anywhere in the cell, for example Cancelled by customer, replace the StrComp test with InStr:

VBA
        If InStr(1, ws.Cells(r, KEY_COL).Text, target, vbTextCompare) > 0 Then
Match cells that contain the text

For a numeric rule such as quantity is zero or negative, test the value instead of the text: If IsNumeric(ws.Cells(r, KEY_COL).Value) And ws.Cells(r, KEY_COL).Value <= 0 Then. Note that an empty cell counts as 0 in that test, so add And Not IsEmpty(ws.Cells(r, KEY_COL).Value) if blanks should stay.

To keep the rows somewhere instead of losing them, copy toDelete to an Archive sheet before deleting: toDelete.Copy Worksheets("Archive").Cells(Rows.Count, 1).End(xlUp).Offset(1). Copying a multi-row Union works as long as all rows are whole rows, as they are here.

On very large sheets (100,000 rows and more), AutoFilter on the column, then deleting SpecialCells(xlCellTypeVisible) below the heading, can be faster still. Find, filter and sort data with VBA shows how.

What can go wrong

Watch out. A macro cannot be undone with Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.

If nothing is deleted although you can see matching rows, check three things: the column letter in KEY_COL, whether the cells contain non-breaking spaces from a web page (they survive Trim$; clean them with VBA macro to trim spaces in selected cells in Excel), and whether a number is displayed with a format such as 1,250.00 while you typed 1250. Because the comparison uses .Text, type the value the way it is shown.

Columns that are too narrow display ####, and then .Text returns the hash signs. Auto-fit the column first. Deleting whole rows also removes data in other columns of those rows, including anything to the right of your table, so keep side notes on a separate sheet.

Tip. Works on Windows and Mac.

Related: VBA macro to delete blank rows in Excel, VBA macro to remove duplicates based on one column in Excel, Find, filter and sort data with VBA.

More vba macros