Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Deleting Every X Rows.

Deleting Every X Rows

Written by Allen Wyatt (last updated September 8, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


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 (2292) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Changing the Company Name

When you install Office or Word, you are asked for your company's name as part of the installation process. If you later ...

Discover More

Calculating Elapsed Time with Excluded Periods

When using Excel to calculate elapsed time, there can be all sorts of criteria that affect the formulas you would ...

Discover More

Spell Checking Only Checking Grammar

Word has a built-in spelling and grammar checker that can help reduce errors in your prose. It may be a bit confusing if ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Selecting All Visible Worksheets in a Macro

Do you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, ...

Discover More

Sheets for Months

One common type of workbook used in offices is one that contains a single worksheet for each month of the year. If you ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

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 nine minus 5?

2018-09-11 05:31:43

Michael (Micky) Avidan

I believe that one loop will be sufficient.

Sub DeleteRows()
Dim iStart As Integer
Dim iCount As Integer
Dim iStep As Integer
Dim J As Integer
iStep = 2 ' Delete every 2nd row from the bottom
Application.ScreenUpdating = 0
iStart = 1
iCount = Selection.Rows.Count
For J = iCount To iStart Step -iStep
Rows(J).Delete
Next
Application.ScreenUpdating = 1
End Sub

-------------
Micky Avidan


2018-09-11 05:23:21

Willy Vanhaelen

@Hank
The minus sign is in fact an n-dash. Replace it with a normal minus and VB will accept it.


2018-09-10 07:20:57

Hank

When I copied the macro, the "iEnd = iEnd — iStep" showed up as a syntax error.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.