To delete the active row with Excel VBA, use ActiveCell.EntireRow.Delete; this removes the entire worksheet row containing the selected cell and shifts rows below it up. For a safer version that asks before deleting, put the macro below in a standard VBA module and run it while the worksheet and target row are active.
What Excel means by the active row
The active row is the worksheet row that contains the active cell—the cell currently selected in Excel. If B7 is selected, for example, VBA can identify row 7 with ActiveCell.Row. The selected cell does not have to be in column A, and the row does not have to contain data.
Deleting that row is different from clearing its contents. A row deletion removes the row’s cells across the worksheet and shifts the rows underneath upward. Clearing contents leaves the row in place, along with its formatting and position. This distinction matters when other columns on the same row contain information: deleting a worksheet row affects them too, even if you were focused on only one column.
When a range of cells is selected, Excel still has an active cell within that selection. A macro using ActiveCell.Row deletes the row containing that one active cell, not every row touched by the selection. If your intention is to remove multiple rows, use a separate macro designed for a selected range rather than assuming this one will remove them all.
A confirmation macro for deleting the active row
This version checks that a worksheet is active, records the active cell’s row number, and asks for confirmation. If you choose No, it exits without making a change.
Sub DeleteActiveRow()
Dim ws As Worksheet
Dim rowNumber As Long
' Make sure the active sheet is a worksheet.
If Not TypeOf ActiveSheet Is Worksheet Then
MsgBox "Activate a worksheet first.", vbExclamation
Exit Sub
End If
Set ws = ActiveSheet
rowNumber = ActiveCell.Row
' Ask before removing the entire worksheet row.
If MsgBox("Delete worksheet row " & rowNumber & "?", _
vbYesNo + vbQuestion, "Confirm deletion") <> vbYes Then
Exit Sub
End If
ws.Rows(rowNumber).Delete
End Sub
The final line deletes the whole row on the active worksheet. The row number is stored before deletion so the confirmation refers to the row you selected. This macro operates on the active sheet, so check the sheet name and selection before confirming. It is not limited to a particular workbook or data range.
If you do not want a confirmation prompt, the essential operation is simply ActiveCell.EntireRow.Delete. A short macro can use that statement, but a prompt helps prevent an accidental click or a run with the wrong cell selected. For a macro used repeatedly, the small amount of extra code is often worthwhile.
Where to put the VBA code and how to run it
In Excel for Microsoft 365 on Windows, open the workbook, press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. Paste the code into the module window. Return to Excel, select a cell in the row to remove, and run the macro using Developer > Macros, select DeleteActiveRow, and choose Run. You can also run it from the Visual Basic Editor with the cursor inside the procedure.
On Mac, use Developer > Visual Basic to open the editor; the keyboard shortcut can vary with the keyboard and function-key settings. Add a module in the editor and run the macro from Excel’s macro list or the editor. Excel for the web does not run VBA macros, so open the workbook in the desktop Excel application to use this code.
Save the workbook in a macro-enabled format such as Excel Macro-Enabled Workbook (.xlsm). A regular .xlsx file does not store VBA code. If Excel displays a security warning, only enable macros for a workbook you trust and expect to run. A macro being present in a file is not, by itself, a reason to enable it.
Delete a worksheet row or only a table row?
The confirmation macro deletes an entire worksheet row, including cells outside a table or data block. That is appropriate when the record spans the whole row and other columns on that row should also move up. It may be too broad when your active cell is inside an Excel table and you want to remove only that table record.
To delete the table row containing the active cell while leaving unrelated worksheet cells in place, use a table-specific procedure. Replace SalesTable with the actual table name, which you can find by selecting a table cell and looking at the Table Design tab. The macro below searches the active sheet’s tables and deletes the matching data row after confirmation.
Sub DeleteActiveTableRow()
Dim ws As Worksheet
Dim lo As ListObject
Dim rowIndex As Long
If Not TypeOf ActiveSheet Is Worksheet Then
MsgBox "Activate a worksheet first.", vbExclamation
Exit Sub
End If
Set ws = ActiveSheet
For Each lo In ws.ListObjects
If Not lo.DataBodyRange Is Nothing Then
If Not Intersect(ActiveCell, lo.DataBodyRange) Is Nothing Then
rowIndex = ActiveCell.Row - lo.DataBodyRange.Row + 1
If MsgBox("Delete this table row?", _
vbYesNo + vbQuestion, "Confirm deletion") = vbYes Then
lo.ListRows(rowIndex).Delete
End If
Exit Sub
End If
End If
Next lo
MsgBox "Select a data cell inside a table first.", vbInformation
End Sub
This table version deletes a data row from the table, not the entire worksheet row. It does not treat the header or totals row as a data record, because it checks the table’s data body. If the table has no data rows or the active cell is outside a table’s data body, it does not delete anything. Use the worksheet-row macro instead when you deliberately want to remove cells across the full sheet.
Run the macro without opening the editor
For occasional use, Excel’s built-in commands may be quicker than a macro. Select a cell in the row, press Shift+Space to select the entire worksheet row, then press Ctrl+- on Windows and choose Delete if Excel presents a deletion choice. You can also use Home > Delete > Delete Sheet Rows. On Mac, the ribbon command is a clear option when the keyboard shortcut differs by keyboard layout or Excel settings.
These commands delete a row immediately or show a prompt depending on the selection and version. A VBA confirmation message is useful if you want a consistent extra check. If you frequently delete rows based on a condition—such as removing records marked Inactive—a macro that checks the condition can be more suitable than deleting whichever row happens to be active. That requires different logic and should be tested on a copy.
Safety, undo, and workbook effects
Save a copy of the workbook before running a deletion macro. Excel’s Ctrl+Z undo command does not reverse changes made by VBA, so you should not rely on the normal undo history to recover a deleted row. If the deletion was a mistake, close the workbook without saving if possible, or restore a saved copy or another available version.
Deleting a row can affect more than visible values. Formatting, formulas, notes, and other cell content in that worksheet row are removed with it. Excel usually adjusts formulas that refer to deleted cells, but the result depends on the formula and workbook structure. A macro also does not automatically know whether the row is important; the confirmation only checks that you approve the deletion, not that the row is safe to remove.
- Check the active worksheet tab and selected cell before running the macro.
- Confirm whether you intend to delete a full worksheet row or just a table record.
- Save a backup copy first, especially in a workbook with formulas or linked sheets.
- Test any modified macro on sample data before using it in a working file.
Common problems and how to fix them
The macro does nothing. In the confirmation version, choosing No exits without deleting. Also verify that you ran the intended macro, that a worksheet is active, and that the workbook is open in desktop Excel rather than Excel for the web.
Excel says macros are unavailable. Check that the file is saved as .xlsm and that your organization’s security settings permit the macro. Do not lower security settings or enable code from an unknown source just to make a macro run.
The wrong row is removed. The macro follows the active cell, which might not be the cell you expect when multiple cells are selected or when you switched sheets. Click a cell in the intended row and verify the sheet name before running it. The confirmation shows the row number, but it does not show the row’s contents.
A table row deletes cells outside the table. That is the result of using EntireRow.Delete or ws.Rows(rowNumber).Delete. Use the table-specific macro if the goal is to remove a table record while preserving unrelated cells elsewhere on the worksheet.
The row will not delete. A protected worksheet can prevent deletion, depending on its protection settings. The macro does not unprotect sheets or bypass protection. Check the sheet’s permissions, and only change protection when you are authorized and know the workbook’s requirements.
Frequently Asked Questions
What is the shortest VBA code to delete the active row?
Use ActiveCell.EntireRow.Delete in a procedure. It removes the worksheet row containing the active cell, so use a confirmation or check the selection first if accidental deletion is a concern.
Does deleting the active row delete the entire row?
Yes, when the code uses EntireRow.Delete or deletes Rows(rowNumber). It removes cells across the worksheet row, not just the active cell or the selected column.
Can I undo a VBA row deletion with Ctrl+Z?
No. Excel’s standard undo command does not undo changes made by VBA. Save a backup before running the macro, or close without saving if you need to discard the change.
How do I delete only the active row in an Excel table?
Use the table’s ListRows(index).Delete method after confirming the active cell is in the table’s data body. That removes the table record without deleting the full worksheet row.
Can I run this VBA macro 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 run VBA.
Related
- Excel Print Area: Set, Adjust, and Troubleshoot
- Excel Page Breaks Keep Appearing? How to Stop Them
- Excel Page Break View Without Page Numbers: What to Do
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Open Workbook VBA: Excel Methods and ExamplesTo open an Excel workbook with VBA, call Workbooks.Open and provide its full file path. For example, assign…
- Excel VBA Active Worksheet Reference ExplainedIn Excel VBA, use ActiveSheet to refer to the sheet currently active in the active workbook. For a…
- Is Excel Macro Recorder Safe to Use? Risks and LimitsExcel’s Macro Recorder is generally safe to use for recording your own routine actions, but it does not…
- Excel VBA UserForm Code Examples and SetupExcel VBA UserForms are custom dialog boxes for collecting information or guiding a task; useful examples include opening…
- Excel VBA UserForm Animation Effects: What Really WorksExcel VBA UserForms have no built-in animation setting for effects such as fading, sliding, or zooming. You can…
- Excel VBA: Insert a Row and Shift Cells DownTo insert a worksheet row in Excel VBA and push the existing rows down, use Rows(rowNumber).Insert Shift:=xlShiftDown on…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.