Excel VBA: Delete a Row If a Cell Is Blank

VBA & macros · Updated November 3, 2026

Excel VBA: Delete a Row If a Cell Is Blank

Yes. In Excel VBA, loop through the rows from the bottom upward and delete a row when the chosen cell is blank. The macro below checks column A, keeps row 1 as a header, and treats a formula returning an empty string or a cell containing ordinary spaces as blank.

What counts as a blank cell?

A cell that has never contained a value is genuinely empty. But “looks blank” can mean other things in Excel: a formula may return an empty string, or a cell may contain spaces that are difficult to see. These cases matter because a simple check such as IsEmpty detects an unused cell but does not treat a formula result of "" as empty.

The macro below uses the cell’s displayed value, then trims ordinary leading and trailing spaces. It therefore considers these cases blank:

  • A cell with no value or formula.
  • A formula cell whose result is an empty string.
  • A cell containing only ordinary spaces, including spaces before or after otherwise empty text.

It does not consider zero, the logical value FALSE, or an Excel error value blank. It also does not remove every invisible character: for example, a nonbreaking space copied from a webpage may remain. If that kind of imported text is common in your data, clean it separately or extend the test to replace that character before trimming.

Use this VBA macro to delete rows

This version checks column A, preserves the first row as a header, and deletes the entire worksheet row whenever the checked cell is blank. It finds the last row containing a value or formula anywhere on the sheet, so it does not rely on column A being continuously populated.

Sub DeleteRowsWhereColumnAIsBlank()
    Dim ws As Worksheet
    Dim lastCell As Range
    Dim lastRow As Long
    Dim rowNum As Long
    Dim cellValue As Variant

    ' Work on the currently selected worksheet.
    Set ws = ActiveSheet

    ' Find the last row with a value or formula on this sheet.
    Set lastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), _
        LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, MatchCase:=False)

    ' Stop if the sheet contains no values or formulas.
    If lastCell Is Nothing Then Exit Sub
    lastRow = lastCell.Row

    ' Work upward so deleting a row does not skip the next row.
    For rowNum = lastRow To 2 Step -1
        cellValue = ws.Cells(rowNum, "A").Value2

        ' Keep error cells; delete cells that are empty after trimming spaces.
        If Not IsError(cellValue) Then
            If Len(Trim$(CStr(cellValue))) = 0 Then
                ws.Rows(rowNum).Delete
            End If
        End If
    Next rowNum
End Sub

The loop begins at the last used row and counts down to row 2. Deleting rows while counting upward can skip records: after a row is deleted, the next row moves into its place, but the loop advances past it. Counting down avoids that problem. The check for IsError also prevents a cell containing an error such as #N/A from causing a conversion error or being mistaken for blank.

Change the column or the header row

To check a different column, change ws.Cells(rowNum, "A") to the column you need, such as ws.Cells(rowNum, "C"). The column letter in this test is independent of the method used to find the last row. The macro still deletes the whole worksheet row, not just the cell in that column.

To keep two header rows, start the loop at row 3 instead of row 2. To allow deletion from the first row, change the lower limit to 1—but do that only if the sheet has no header or other content that must be retained. If the sheet has a known, fixed data range, you can also set the last row from that range; the Find method in the example is useful when the data length changes between runs.

For a rule involving more than one cell, decide precisely what “blank” means before changing the test. For instance, deleting a record only when all required fields are blank is different from deleting it when any one field is blank. The latter can remove valid records that simply lack an optional value. Check the rule on a few sample rows before running it across the full list.

Add and run the macro in Excel

  1. Save a backup copy of the workbook. A row-deleting macro changes the data, and macro actions cannot be reversed with Ctrl+Z.
  2. Open the workbook in the desktop version of Excel and select the worksheet you intend to clean. The code uses ActiveSheet, so the selected sheet matters.
  3. Open the Visual Basic Editor. On Windows, press Alt+F11 or choose Developer > Visual Basic. On Mac, use Option+F11 or choose Developer > Visual Basic; depending on your keyboard settings, you may also need to hold Fn.
  4. In the editor, choose Insert > Module, paste the code into the module, and return to Excel.
  5. Run the macro using Alt+F8 on Windows or Option+F8 on Mac, select DeleteRowsWhereColumnAIsBlank, and choose Run.

If the Developer tab is not visible, enable it in Excel’s ribbon settings. On Windows, use File > Options > Customize Ribbon and select Developer. On Mac, open Excel > Settings > Ribbon & Toolbar and enable Developer. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you need to keep the VBA code. Excel for the web can open a macro-enabled workbook, but it cannot run VBA macros; use desktop Excel to execute this procedure.

Why the macro counts backward

Deleting a worksheet row shifts all rows below it upward immediately. Suppose rows 4 and 5 both have blank cells in column A. If a forward loop deletes row 4 and then moves to row 5, the original row 5 has already shifted into row 4. The loop may therefore leave that blank record behind. A backward loop checks row 5 first and row 4 next, so the shift does not affect rows still waiting to be checked.

The last-row search looks for a value or formula across the worksheet, rather than stopping at the last nonblank cell in column A. This is useful when column A has gaps but other columns contain data farther down. Formatting alone does not normally count as a value or formula for this search. If your records are on a separate worksheet, or if you want the macro to target a particular sheet regardless of selection, replace ActiveSheet with a specific worksheet reference in the workbook.

Alternatives to deleting rows with VBA

VBA is useful when the cleanup must be repeated, the dataset is long, or the deletion rule is part of a larger workflow. For a one-time cleanup, Excel’s built-in tools may be easier to review. You can filter the relevant column to show blank cells, inspect the visible records, select their worksheet rows, and delete them. Filtering makes it possible to verify which records will be removed before taking action, although care is needed to select entire worksheet rows rather than just the visible cells in one column.

Another option is to sort or filter the data, copy the records you want to keep to a new range, and leave the original data untouched until you have checked the result. That approach is safer when the blank-cell rule is complicated or the source data must be preserved. For repeated imports, a macro can be more consistent, but it should be tested on a copy and should clearly state which sheet, column, and rows it will affect.

Common problems and how to avoid them

  • The wrong sheet changes: The example uses ActiveSheet. Select the intended sheet immediately before running it, or edit the code to name the target worksheet explicitly.
  • Rows that look blank remain: The cell may contain a nonbreaking space, a tab, or another invisible character. Ordinary VBA Trim$ removes leading and trailing spaces, not every possible character copied into a cell.
  • A formula result of zero is deleted unexpectedly: Zero is not blank in this macro. If zero should count as blank for your data, make that a deliberate rule rather than assuming Excel treats zero as empty.
  • Some data below the expected range is missed: Confirm the relevant records are on the worksheet being checked and that the search can find their values or formulas. If the dataset has a fixed boundary, use that range intentionally.
  • Other content disappears or shifts: ws.Rows(rowNum).Delete deletes the entire worksheet row. Any unrelated content on that row, including notes or other tables, is affected too.

Filtered lists, merged cells, and Excel tables deserve extra attention. Merged cells can make it unclear which row owns a value, while a worksheet-row deletion can affect content beside a table as well as the table itself. If the data is an Excel table and you mean to remove only table records, verify the result on a copy or use a table-specific approach suited to that table. Always inspect the affected area after a small test run.

Safety before running a delete macro

Deleting rows with VBA is a direct change, not a preview. Ctrl+Z does not undo a macro’s changes, so save a separate copy before running it. For important workbooks, test the macro on a duplicate sheet or a small sample first, then compare the remaining records with the rule you intended to apply.

Also consider formulas and references elsewhere in the workbook. Excel adjusts many ordinary cell references when rows are deleted, but the result can still change calculations, charts, named ranges, or linked data in ways that matter to your workbook. A backup lets you restore the original if the outcome is not what you expected. Keep macros disabled unless you trust the workbook and understand what its code does.

Frequently Asked Questions

Can VBA delete a row if a formula returns an empty string?
Yes. The example reads the formula’s result with Value2. If that result is an empty string, the trimmed value has zero length and the row is deleted.

Does this macro delete rows when the cell contains zero?
No. Numeric zero converts to the text "0", which has a length greater than zero. The macro treats zero as a value, not as a blank.

How do I check column B instead of column A?
Change the column argument in the cell test from "A" to "B". The macro will still delete the entire worksheet row when the cell in column B meets the blank test.

Can I undo a VBA row deletion with Ctrl+Z?
No. Excel’s normal Undo history does not reverse changes made by a VBA macro. Save a backup copy before running the code so you can restore the original data if needed.

Will this macro run in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to use the Visual Basic Editor and run the procedure.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.