To delete every other row in Excel with VBA, select the rows to process and run a macro that deletes the second, fourth, and subsequent even-numbered rows within that selection. The macro below works from the bottom upward, so row deletions do not shift the remaining target rows before they are processed.
VBA macro for deleting alternating rows
This version keeps the first row in your selection and deletes every second row after it. Select a contiguous range that covers the data rows you want to affect; the macro deletes entire worksheet rows, not just the cells in the selected columns.
Sub DeleteEveryOtherRowInSelection()
Dim target As Range
Dim ws As Worksheet
Dim firstRow As Long
Dim rowCount As Long
Dim i As Long
Dim oldScreenUpdating As Boolean
' Require one contiguous cell selection.
If TypeName(Selection) <> "Range" Then Exit Sub
Set target = Selection
If target.Areas.Count <> 1 Then
MsgBox "Select one contiguous range of rows first.", vbExclamation
Exit Sub
End If
Set ws = target.Worksheet
firstRow = target.Row
rowCount = target.Rows.Count
If rowCount < 2 Then
MsgBox "Select at least two rows.", vbInformation
Exit Sub
End If
If MsgBox("Delete every second row in the selection, keeping its first row?", _
vbYesNo + vbQuestion, "Confirm row deletion") <> vbYes Then Exit Sub
On Error GoTo HandleError
oldScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
' Work upward so deleting a row does not change lower target row numbers.
For i = rowCount To 1 Step -1
If i Mod 2 = 0 Then ws.Rows(firstRow + i - 1).Delete
Next i
Application.ScreenUpdating = oldScreenUpdating
MsgBox "Finished deleting alternating rows.", vbInformation
Exit Sub
HandleError:
Application.ScreenUpdating = oldScreenUpdating
MsgBox "The macro stopped: " & Err.Description, vbExclamation
End Sub
Save a copy of the workbook before running any deletion macro: VBA actions cannot be undone with Ctrl+Z. If an error occurs partway through, the error message helps identify the issue, but it does not restore rows that were already deleted.
How the macro chooses which rows to delete
The code numbers the selected rows relative to the selection, starting at 1. It deletes rows with an even relative number and keeps rows with an odd relative number. For example, if you select A2:D8, the selection contains seven worksheet rows: row 2 is relative row 1, row 3 is relative row 2, and so on. The macro deletes worksheet rows 3, 5, and 7, while keeping rows 2, 4, 6, and 8.
The loop starts at the bottom of the selection and moves upward. That order matters. If the macro started at the top, deleting a row would move all the rows below it up by one, changing which row number the next loop iteration referred to. Processing from bottom to top avoids that shifting problem while still following the original selection’s pattern.
The test i Mod 2 = 0 identifies even-numbered positions. To delete the first, third, fifth, and other odd-numbered positions instead, change it to i Mod 2 = 1. That change also means the first row of the selected range will be deleted, so confirm that the range does not include a header or a row you need to keep.
Choose the range and protect the header
Decide what “every other row” means for your sheet before running the macro. If row 1 contains column headings and your records begin in row 2, select only the record rows. For example, selecting A2:F101 processes 100 data rows while leaving the header outside the selection. Selecting whole rows is also possible, but it is not required: any contiguous cell range spanning the intended rows gives the macro its starting row and row count.
Because the macro deletes entire worksheet rows, selecting only column A does not limit the deletion to column A. If you select A2:A20, Excel removes those worksheet rows across all columns, including any data in B, C, or farther to the right. This is useful when each record spans multiple columns, but it can remove unrelated information stored on the same rows. Check the whole sheet before proceeding.
- Use a contiguous selection; the macro deliberately rejects a selection made of separate areas.
- Exclude title rows, headings, totals, and notes that must remain.
- Check whether formulas or records in other columns share the selected worksheet rows.
- If you want to keep a different starting row, change the even/odd test rather than shifting the selection accidentally.
Run the macro in Excel for Microsoft 365
On Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste the code into the module. Return to the worksheet, select the intended range, and run the macro with Alt+F8: choose DeleteEveryOtherRowInSelection and select Run. You can also run it from the Visual Basic Editor. If the Developer tab is not visible, enable it through File > Options > Customize Ribbon and select Developer.
On Mac, open the Visual Basic Editor from Developer > Visual Basic; the keyboard shortcut is usually Option+F11, sometimes with Fn as well depending on the keyboard. Use Developer > Macros to run the procedure. The macro’s row-deletion logic is the same, though ribbon and shortcut availability can differ by Excel build and keyboard settings. Excel for the web can open a macro-enabled workbook, but it does not run VBA macros; use the desktop Excel app.
Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) if you need to keep the VBA code in the file. If Excel displays a security warning, only enable macros in a workbook you trust. Do not change global macro-security settings merely to run an unfamiliar file.
Change the starting pattern or delete rows by another rule
“Every other row” can mean different patterns. The macro’s default is to keep the first selected row, then delete the next one. If you want to delete the first selected row and keep the second, use the odd-position test described above. If you need to keep a header while deleting alternating records, the simplest approach is to leave the header out of the selection; that makes the first data row the first position in the pattern.
A fixed alternating pattern is different from deleting rows that meet a condition. If the goal is to remove blank records, rows with a particular status, or records older than a date, use a rule based on the relevant cells rather than row position. Otherwise, a row that should stay could be deleted simply because it happens to occupy an even position. For complicated criteria, make a helper column that marks rows to remove, review the marks, and then delete the marked rows.
For a very large dataset, consider whether the pattern should be based on the worksheet’s physical row positions or on the current visible records. The sample uses worksheet rows in the selected range. It is not designed to skip hidden rows or to interpret a filtered list as a new consecutive sequence.
Filtered, hidden, and grouped rows
Take extra care when a filter is active. The macro does not inspect the visible cells; it calculates row numbers from the selection and deletes entire worksheet rows at those positions. With hidden or filtered records, the visible sequence may not match the underlying row sequence, so the result can differ from “delete every other visible record.” Clear filters or use a method specifically built around the visible records if that is what you intend.
Manually hidden rows and grouped rows also deserve a preview. A hidden row still has a worksheet row number and can be included in a contiguous selection. Deleting worksheet rows may affect the surrounding outline or the structure of a table. If you use Excel’s filter or outline features, test on a copy and check the final data, formulas, and grouping before saving over the original.
Common problems and safe checks
If the macro appears to do nothing, check that you selected at least two rows and accepted the confirmation prompt. If you see the message asking for one contiguous range, select a single rectangular block rather than holding Ctrl or Command to select separated areas. Also verify that you are running the procedure in desktop Excel, not Excel for the web.
If the wrong rows are removed, the usual cause is a mismatch between the desired starting pattern and the selected range. Re-check whether the first selected row should be kept or deleted, and whether a header was accidentally included. A small test on a copied worksheet with a short sequence of clearly numbered rows is a practical way to confirm the pattern before applying it to important data.
Deleting whole rows can affect formulas, named ranges, charts, and references elsewhere in the workbook. Excel commonly adjusts references when rows are deleted, but that does not guarantee every dependent process or external link will behave as intended. Review key formulas and reports after the operation. Ctrl+Z cannot reverse VBA changes, which is why a separate workbook copy is the most dependable recovery option.
Alternatives to a deletion macro
For a one-time job on a small sheet, a helper column may be easier to inspect than VBA. Add an index or a formula that marks alternating records, fill it down, filter for the rows marked for removal, and delete the visible worksheet rows after checking the results. This adds a review step and makes the intended pattern visible, but deleting filtered records still requires care; confirm which rows are selected before removing entire rows.
If you regularly need to remove alternate records, a macro saves repetitive selection work. If you only need to show every other record rather than permanently remove data, filter or copy the chosen records to another sheet instead. Keeping the source intact is safer when the deletion is only for presentation, sampling, or a temporary analysis.
Frequently Asked Questions
Does this macro keep the first row in the selection?
Yes. It keeps relative rows 1, 3, 5, and so on, and deletes relative rows 2, 4, 6, and so on. Change the test to i Mod 2 = 1 if the first selected row should be deleted.
Can I delete every other row but keep the header?
Yes. Leave the header out of the selection and select only the data rows. The macro’s pattern then starts with the first selected data row, which it keeps.
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 separate copy before running it, especially on a workbook with important formulas or linked reports.
Will it delete only the selected cells?
No. It deletes entire worksheet rows across all columns. Select the correct row range and check for unrelated data elsewhere on those rows before confirming.
Does the macro work in Excel for the web?
No. Excel for the web does not run VBA. Open the macro-enabled workbook in the desktop version of Excel for Microsoft 365 on Windows or Mac to run the procedure.
Related
- Microsoft Excel Print Area: Set, Change, and Clear
- Microsoft Excel Protect Sheet: Steps, Limits, and Tips
- Microsoft Excel Named Ranges: How They Work and Help
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Filter Dictionary Keys in Excel VBA: Code and OptionsYes. In Excel VBA, filter a Scripting.Dictionary by looping through its keys and copying the matching key/value pairs…
- Excel VBA: Find a Character’s Position in a StringIn Excel VBA, use InStr to find a character’s position in a string: it returns the character’s one-based…
- VBA Array Search: Find Values with Loops or MatchTo search a VBA array, loop from LBound to UBound, compare each element with the value you want,…
- VBA Sort Not Working? Fix Excel Sorting Macros with Clear RangesIf VBA sorting is not working, check that the macro is running, the sort range includes every column…
- VBA Find After: How Excel Chooses the Search Start CellIn Excel VBA, the After argument in Range.Find tells Excel which single cell to search after when beginning…
- Excel VBA Find Numbers Only: Code, Search Tips, and FixesYes. To find cells that contain real numeric values—not numbers stored as text—loop through the range and check…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.