Wauvel

Excel like a finance pro.

The functions, VBA, and shortcuts a finance operator actually uses — curated, with CFO examples. Prefer them pre-built? Grab a ready-made model.

← All VBA snippets

Delete rows that meet a condition

Loop from the bottom up and delete matching rows — the safe way that never skips one.

1What it does

Walks the used rows from the bottom to the top and deletes each one that matches a rule (here, rows marked 'Void' in column D). Direction matters: deleting a row shifts everything below it up, so a top-down loop jumps over the row that slid into the gap. Counting DOWN with Step -1 avoids that entirely.

2The code

Sub DeleteRows()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Delete BOTTOM-UP. Top-down, deleting row 5 shifts row 6 up into 5 — and
    ' the next step skips it. Counting down sidesteps that.
    For i = lastRow To 2 Step -1
        If ws.Cells(i, "D").Value = "Void" Then   ' e.g. remove voided rows
            ws.Rows(i).Delete
        End If
    Next i
End Sub

Paste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).

New to macros? Set up in 5 minutes
  1. 1

    Don't see the Developer tab in the ribbon?

    You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.
    • Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
    • Mac:Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
  2. 2

    Paste in the code

    Press Alt + F11 to open the Visual Basic editor, then Insert → Moduleand paste the snippet's code into the blank window. Close it with Alt + Q.
  3. 3

    Run it

    Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in: =GrossMargin(B2, B3).)
  4. 4

    Keep the macro — save as .xlsm

    File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save.
  5. 5

    Macros blocked?

    Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.

Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.

3When you use it

  • Strip out voided, cancelled, or zero-dollar rows from an export.
  • Remove subtotal or blank separator rows before analysis.
  • Keep only the rows that match a status, region, or account.

4See it in action

A simulation — press Run to perform what the macro does to a sample workbook.

2INV-01Paid
3INV-02Void
4INV-03Paid
5INV-04Void
6INV-05Paid

5Pitfalls

Looping top-down misses every other match (it skips the shifted rows).

Fix: Loop bottom-up: For i = lastRow To 2 Step -1.

Deleting one row at a time is slow on big sheets.

Fix: For thousands of rows, filter to the matches with AutoFilter and delete the visible ones in a single operation.

6No-code alternatives

  • AutoFilter + delete visible Filter to the matching rows, then delete the visible ones at once — much faster at scale.
  • Sort then delete Sort the condition to the bottom and delete the block as one range.

Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.

Learn the moves here — or let Wauvel run them on your numbers.

Get my free report →