Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Deleting Every X Rows.

Deleting Every X Rows

Written by Allen Wyatt (last updated March 16, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


3

When you import data from an outside source, you may run into a need to delete extraneous data from a worksheet. For instance, you may have a need to remove every second line from the data, or every fifth line. Doing this by hand can be tedious and prone to error. Fortunately, you can create a macro to help eliminate both the tedium and the errors.

The following macro, DeleteRows, will remove every X rows from your worksheet. All you have to do is select the rows you want it applied to. The macro, as written, will remove every second row. So, if you wanted to delete the first, third, fifth, and seventh rows beginning with row 10, you would select rows 10 through 16 and then run this macro. It results in rows 10 (the first row), 12 (the third row), 14 (the fifth row), and 16 (the seventh row) being deleted.

Sub DeleteRows()
    Dim iStart As Integer
    Dim iEnd As Integer
    Dim iCount As Integer
    Dim iStep As Integer
    Dim J As Integer

    iStep = 2    'Delete every 2nd row
    Application.ScreenUpdating = False
    iStart = 1
    iCount = Selection.Rows.Count
    'Find ending row to start deleting
    For J = iStart To iCount Step iStep
        iEnd = J
    Next

    Do While iEnd >= iStart
        Selection.Rows(iEnd).Delete
        iEnd = iEnd — iStep
    Loop
    Application.ScreenUpdating = True
End Sub

If you want to delete some other multiple of lines, simply change the setting for the iStep variable. For instance, if you want to delete every fifth row, change iStep from 2 to 5. (You only need to make the single change, in the iStep = 2 declaration.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3592) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Deleting Every X Rows.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using the Same Range Name on Different Worksheets

Defined names can be a great boon when working in a worksheet. Usually names are available throughout an entire workbook, ...

Discover More

Protecting a Single Worksheet

Excel allows you to protect your worksheets easily—and that includes if you need to protect only a single worksheet ...

Discover More

Restarting Footnote Numbering after Page Breaks

Footnotes can be handled many different ways in a document. If you want to restart the numbering of your footnotes every ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Putting the Last Saved Date in a Cell

Do you need a cell in your worksheet to display the date on which the workbook was last saved? This can be a bit tricky, ...

Discover More

Friendly and Informative Error Handling

When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's ...

Discover More

Specifying Location for a Message Box

When writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2024-03-25 12:14:05

J. Woolley

Re. my previous comment below, My Excel Toolbox now includes the following two macros:
    DeleteEveryNthRow -- Delete every Nth row of the selected range
    DeleteEveryNthCol -- Delete every Nth column of the selected range
See https://sites.google.com/view/MyExcelToolbox/


2024-03-19 18:30:50

J. Woolley

Here's my version of the Tip's macro:

Sub DeleteRows2()
    Const myName = "DeleteRows2"
    Dim nStart As Long, nCount As Long, nEnd As Long, nStep As Long
    Dim nSelecRows As Long, rSelecCells As Range, rDelete As Range
    Dim n As Long, msg As String
    Set rSelecCells = Selection
    For n = 1 To rSelecCells.Areas.Count 'might be non-contiguous
        nSelecRows = nSelecRows + rSelecCells.Areas(n).Rows.Count
    Next n
    Selection.Areas(1).EntireRow.Select 'selection of contiguous rows
    nStart = Selection.Cells(1).Row
    nCount = Selection.Rows.Count
    nEnd = nStart + nCount - 1
    msg = "To delete every Nth row of the selected range " & vbLf _
        & "$" & nStart & ":$" & nEnd & " starting with row $" _
        & nStart & ", enter a value for " & vbLf & "increment N:"
    nStep = Int(Application.InputBox(msg, myName, 0, Type:=1))
    If nStep < 1 Then
        rSelecCells.Select
        MsgBox "No rows were deleted.", , myName
        Exit Sub
    End If
    nEnd = nStart + nStep * ((nCount - 1) \ nStep) 'integer division
    Set rDelete = Rows(nStart).EntireRow
    For n = (nStart + nStep) To nEnd Step nStep
        Set rDelete = Application.Union(rDelete, Rows(n).EntireRow)
    Next n
    rDelete.Select
    msg = "The selected rows will be deleted."
    If MsgBox(msg, vbOKCancel, myName) = vbOK Then
        nSelecRows = nSelecRows - rDelete.Rows.Count
        rDelete.Delete
    End If
    If nSelecRows > 0 Then rSelecCells.Select Else ActiveCell.Select
End Sub


2024-03-17 01:40:32

Nir

If VB is too much for you:
add the following in a new column:

=MOD(ROW()-X,Y)

1. where X is the first data row (after headers) and Y is the number of rows to count.
2. first row for the formula=first DATA row.
3. drug all the way down.
4. filter OUT the zeros.
5. delete all the rows and remove filter.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.