To delete the worksheet row containing the active cell in Excel VBA, call the row’s Delete method. The basic macro below does that; save a copy of your workbook first, because a macro deletion cannot be undone with Ctrl+Z.
Sub DeleteActiveRow()
Dim ws As Worksheet
Dim rowNumber As Long
' Use the worksheet that contains the active cell.
Set ws = ActiveSheet
rowNumber = ActiveCell.Row
' Delete the entire worksheet row.
ws.Rows(rowNumber).Delete
End Sub
This is for desktop Excel, including Microsoft 365 for Windows. Excel for the web cannot run VBA macros. If the active cell is inside an Excel table and you intend to remove only that table record, use the table-specific approach below instead.
What “active row” means in Excel
The active cell is the single cell in the current selection that Excel treats as the focus. A selection can contain many cells, but it still has one active cell, usually indicated by the white cell within the highlighted selection. In VBA, ActiveCell.Row returns that cell’s worksheet row number. The number is an integer from 1 through the worksheet’s last row, not a row position within a table.
Deleting the active row normally means deleting the entire worksheet row, not merely clearing the active cell or removing the cells in the current selection. The macro uses the worksheet’s row collection and deletes the row at the active cell’s row number. If the active cell is D12, for example, it deletes worksheet row 12 across all columns, including cells that appear empty.
That distinction matters when several cells or a whole range are selected. The basic macro still uses the row containing the active cell; it does not delete every row touched by the selection. If your intention is to remove a set of rows, first define how those rows should be identified rather than assuming that a multi-cell selection changes what this macro does.
How the basic macro works
ActiveSheet refers to the sheet currently displayed and active in the workbook. The code stores it in a Worksheet variable, then saves ActiveCell.Row in a Long variable. A Long is appropriate for worksheet row numbers and avoids the limitations of older Integer variables. Finally, ws.Rows(rowNumber).Delete removes that row from the sheet.
Saving the row number before deletion makes the target explicit and easier to inspect while debugging. The shorter expression ActiveCell.EntireRow.Delete also deletes the full row containing the active cell, but the longer version makes the worksheet and row number visible as separate steps. Both approaches operate on the worksheet row, not just the cells currently selected.
The macro assumes that a worksheet is active and that the sheet is not protected against row deletion. If the active window is showing a chart sheet rather than a worksheet, assigning ActiveSheet to a Worksheet variable can fail. For a macro used by other people or in a workbook with multiple sheet types, add a check that the active sheet is a worksheet before attempting deletion.
How to add and run the macro
- Save a backup copy of the workbook. Macros that delete rows cannot be reversed with Excel’s Undo command.
- Save the working file as an Excel Macro-Enabled Workbook (.xlsm) if it contains VBA you want to keep.
- Open the Visual Basic Editor from Developer > Visual Basic, then choose Insert > Module in the editor and paste the macro into the module.
- Return to Excel, click a cell in the row you intend to remove, and run the macro using Developer > Macros. Select DeleteActiveRow and choose Run.
If the Developer tab is missing in Windows, enable it through File > Options > Customize Ribbon and select Developer. On Mac, enable the tab through Excel > Settings > Ribbon & Toolbar. Excel for Mac supports VBA, although its menus and keyboard shortcuts can differ; using the Developer tab’s Visual Basic and Macros commands avoids relying on a platform-specific shortcut. Excel for the web can open an .xlsm workbook, but it does not run the VBA code.
Only enable macros in files you trust. A macro can change or delete workbook content, so do not run code from an unknown source just because Excel displays a prompt. If the workbook is stored in a managed workplace environment, your organization may also restrict macros through its security policies.
Add a confirmation before deleting
A confirmation prompt is useful when a macro is run manually, especially if the active cell can move between the time you select a row and the time you launch the macro. This version asks before deleting and identifies the worksheet row number. It still deletes the entire worksheet row if you confirm.
Sub ConfirmAndDeleteActiveRow()
Dim ws As Worksheet
Dim rowNumber As Long
Dim answer As VbMsgBoxResult
' Identify the active worksheet and row.
Set ws = ActiveSheet
rowNumber = ActiveCell.Row
' Continue only when the user confirms.
answer = MsgBox("Delete worksheet row " & rowNumber & "?", _
vbYesNo + vbQuestion, "Confirm row deletion")
If answer <> vbYes Then Exit Sub
' Delete the selected worksheet row.
ws.Rows(rowNumber).Delete
End Sub
A prompt reduces accidental clicks but is not a substitute for a backup. It confirms only the row number, not whether the row contains important records, formulas, or formatting. For a high-impact process, display the worksheet name as well, or build a review step that shows the values in the row before removing it.
Deleting a record from an Excel table
An Excel table is a structured list with its own data rows, headers, and possibly a totals row. Deleting an entire worksheet row because the active cell is in a table can also remove cells outside the table in that same row. If you want to remove only the record from the table, delete the corresponding ListRow instead. This keeps the action focused on the table data rather than the whole sheet row.
The following example assumes the table is named Table1 and is on the active worksheet. In the worksheet, select a cell in the table’s data body—not its header or totals row—before running it. You can see or change a table’s name on the Table Design tab when a table cell is selected.
Sub DeleteActiveTableRecord()
Dim ws As Worksheet
Dim tbl As ListObject
Dim tableRowNumber As Long
' Set the table name used in this workbook.
Set ws = ActiveSheet
Set tbl = ws.ListObjects("Table1")
' Stop if the table has no data rows or the active cell is outside its body.
If tbl.DataBodyRange Is Nothing Then Exit Sub
If Intersect(ActiveCell, tbl.DataBodyRange) Is Nothing Then Exit Sub
' Translate the worksheet row into a table-row position.
tableRowNumber = ActiveCell.Row - tbl.DataBodyRange.Row + 1
tbl.ListRows(tableRowNumber).Delete
End Sub
If the table has no data rows, DataBodyRange is Nothing, so the check prevents the code from trying to identify a record that does not exist. The Intersect check also prevents an unrelated active cell from being treated as a table record. Change Table1 to the actual table name; if the table is on another worksheet, set ws to that worksheet explicitly rather than relying on ActiveSheet.
What deleting a worksheet row changes
Deleting a worksheet row shifts rows below it upward. It does not simply blank the row. Values, formulas, formatting, and other cell content in the deleted row are removed, and any content below moves to a different row number. This can affect references and the layout of data elsewhere on the sheet, so inspect the whole row rather than only the active cell before running the macro.
Excel adjusts many formula references when rows are deleted, but a formula that refers directly to deleted cells can become a #REF! error. A range reference may be adjusted to reflect the remaining cells, depending on what was deleted and how the formula is written. Check formulas, charts, named ranges, and downstream reports after a structural change, particularly in a workbook that uses fixed row positions or external links.
Rows hidden manually or hidden by a filter are still worksheet rows. Deleting one row by its number affects that row even if it is not currently visible. When a filter is active, be deliberate about whether you want to delete one record, all visible records, or a full worksheet row: those are different operations. A table’s ListRows collection is often the clearer choice for removing a filtered table record.
Common problems and safeguards
- The wrong row disappears: ActiveCell.Row follows the active cell, not necessarily the row you meant to target. Click a cell in the intended row immediately before running the macro, or add a confirmation that includes the sheet and row.
- Excel reports that it cannot delete the row: The worksheet may be protected, or the workbook’s structure or permissions may restrict changes. Review the protection settings and obtain authorization before changing them; do not assume the macro should bypass a protection policy.
- The macro fails on a table: The table may have a different name, or the active cell may be in its header or totals row. Confirm the table name and select a cell in its data body. Use the table-record example if only the record should be removed.
- The macro does not appear in the Macros list: Check that the code is in a standard module, the file is saved as .xlsm, and macros are permitted by the workbook’s security settings. Code placed in a worksheet’s code module may not appear like a standard public macro.
- Undo does not restore the row: This is expected for VBA changes. Ctrl+Z does not undo a macro’s deletion, which is why saving a separate copy before running it is essential.
For a macro that other people will use, consider validating the active sheet and cell before deletion, and provide a clear message when the selection is not suitable. Avoid adding automatic deletion to an event procedure unless the consequences are fully understood: event code can run in response to ordinary workbook actions and may make an unintended change difficult to notice.
When to use a manual command instead
If this is a one-time edit, a macro may be unnecessary. Select a cell in the desired row, then use Home > Delete > Delete Sheet Rows. This is explicit about deleting the worksheet row. If you select cells and use the general Delete command, Excel may instead offer choices such as shifting cells, so verify the command before confirming.
Use VBA when the action must be repeated, attached to a button, or included in a larger workflow. Use the table-specific method when the unit you mean to remove is a table record rather than an entire worksheet row. Use a manual command when you want to inspect the deletion in the interface and do not need repeatable automation. In every case, distinguish deleting a row from clearing its contents: clearing leaves the row in place and does not shift the rows below it.
Frequently Asked Questions
Does this delete the active cell or the whole row?
The basic macro deletes the entire worksheet row containing the active cell. It removes content across the row and shifts rows below upward; it does not merely clear the active cell.
What if several rows are selected?
The basic macro deletes only the worksheet row containing ActiveCell, even if the selection spans several rows. To delete multiple rows, define and validate the full target range rather than relying on the active cell alone.
Can I undo a VBA row deletion with Ctrl+Z?
No. Excel’s normal Undo command cannot reverse a VBA macro’s row deletion. Save a separate copy before running destructive code and verify the target before confirming.
Can I run this macro in Excel for the web?
No. Excel for the web does not execute VBA macros. Open the workbook in the desktop Excel app to run VBA; Excel for Mac supports VBA through its desktop app as well.
How do I delete only a table row?
Use the table’s ListRows collection to delete the record at the active cell’s position within the table body. The table example above checks that the cell is in the data body and avoids deleting the rest of the worksheet row.
Related
- Microsoft Excel Insert Page Break: A Practical Guide
- Microsoft Excel Heat Maps: How to Create and Read Them
- Microsoft Excel: How to Remove Duplicate Rows Safely
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA MsgBox: Create an OK or Decline Box?Yes. In VBA, use MsgBox with the vbOKCancel button style to show OK and Cancel, or vbYesNo to…
- VBA MsgBox Location: Where It Appears and How to Control ItA VBA MsgBox has no built-in argument for setting its screen coordinates. In Excel for Microsoft 365 on…
- VBA Find and Replace: Methods, Examples, and Tips in ExcelIn Excel VBA, use Range.Find to locate matching cells and Range.Replace to change matching content. For example, Worksheets("Data").Range("A1:D100").Replace…
- Excel VBA: Find the Bottom Row of Data in a SheetTo find the bottom row in a particular Excel column, start at the worksheet’s last row and move…
- Excel VBA Find Cell: Code, Options, and ExamplesIn Excel VBA, use the Range.Find method to locate a cell containing specified text or a value. Set…
- Excel VBA: Bring a Workbook Window to the Front ReliablyTo bring an open workbook to the front with Excel VBA, activate Excel and then activate the workbook:…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.