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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially ...
Discover MoreIf you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...
Discover MoreEver wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.
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