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.
Written by Allen Wyatt (last updated September 8, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Does your macro need to change the width of some columns in a worksheet? Here's how to do it.
Discover MoreNeed to know how many characters there are in a workbook? You can find out easily with the handy macro introduced in this ...
Discover MoreNeed your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments