Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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

by Allen Wyatt
(last updated May 20, 2016)


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

    Do While iEnd >= iStart
        iEnd = iEnd - iStep
    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.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3592) applies to Microsoft Excel 2007, 2010, and 2013. 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. ...


Making Word Stop at the End of a Document when Reviewing Changes

The Track Changes tool is a great help when you want to see what edits have been made to a document. When you review ...

Discover More

Linking Comments to Multiple Cells

In Excel, single comments are associated with single cells. If you want to have a comment be linked to multiple cells, ...

Discover More

Quickly Customizing the Keyboard

Want a quick way to change the shortcut key associated with a tool available on a ribbon, toolbar, or menu? Here's one ...

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 (ribbon)

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

Discover More

Aborting a Macro and Retaining Control

If you need to exit a macro before it is finished running, you can do it using a brute force method, or you can build in ...

Discover More

Page Numbers in VBA

When you print a larger worksheet, Excel breaks the printout across several pages. You may want to know, before you ...

Discover More

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

View most recent newsletter.


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}] 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 1 + 5?

2016-05-21 05:31:16


Mark, A technique I use a lot isn't a macro.

Filter your data & select the rows that contain the identifying character or string; use "begins with", "contains" or "ends with" as appropriate. [If numeric, use other appropriate selections).

One benefit is you can select/filter on multiple columns. eg. a status in one column and a special character in another column.

Once you have the rows you want to delete; select the rows (I usually click on the last row, press [end] then up arrow, hold [shift] down whilst clicking on the first row I want to delete to select all the rows, then right click and select "delete".

Quite often, I import reports and want to remove headings etc. so I filter & delete multiple times. With this technique, you can always keep the first selected row(s) (eg. the first set of headings) and just delete the rest.

Depending on your requirements, you could create a macro to do this for you.

Hope that helps.


2016-05-20 10:47:49



Thanks for this useful macro. Is it possible to delete random rows which will all have the same identifying character?



2013-08-24 09:34:37


That should work as a quick and dirty way to do it.

2013-08-23 14:15:10


Bryan, you are correct. Ooops! and I knew that too. Next time I will test stuff before posting.

Sub DeleteRows()
Dim RowNum As Long

For RowNum = 100 To 1 Step -2
ActiveSheet.Rows(RowNum & ":" & RowNum).Delete Shift:=xlUp
Next RowNum

End Sub

... is the corrected way.

Of course which set of rows, when to start and finish, how many rows, etc. need to be determined by your specific needs.

2013-08-21 11:34:36


Aldo, yours won't work like you want. When you delete a row the subsequent rows move, so 2 becomes 3, 3 becomes 4, etc. You need to step backwards through the rows, but make sure you will end on the right row.

2013-08-21 10:09:53


There is a lot of code here to do something very simple ... deleting rows.

Sub DeleteRows()
Dim RowNum As Long

For RowNum = 1 To 100 Step 2
ActiveSheet.Rows(RowNum & ":" & RowNum).Delete Shift:=xlUp
Next RowNum

End Sub

Of course which row to start, which row to end, and the step need to be determined. With this you can even delete groups of rows by changeing the second RowNum value in .Rows(RowNum & ":" & RowNum)

2013-08-20 11:16:44

John K

@ Bryan

Your correction seems to be working fine. Thanks!

2013-08-19 15:59:20


@John -- sorry about the delay. If you still need it, here's what you would do:

"Set rngDelete = Selection.Cells(1, 1)"
"Set rngDelete = Selection.Cells(istep, 1)"

And then change
"For J = 1 + iStep To Selection.Rows.Count Step iStep"
"For J = iStep * 2 To Selection.Rows.Count Step iStep"

At least I think so; I didn't double check. If you select a step of 3 and only select 1 row, it will still delete a row so be careful.

2013-07-24 14:47:14

John K


I am trying to implement an InputBox for iStep into your code so I can delete every nth row, n = iStep.

iStep = InputBox("Delete every nth row. n = ", "Delete every nth row")

This works but it also deletes the first row as well, which really shouldn't happen. For example, if I use n = 3 over 15 rows, rows 1, 3, 6, 9, 12, 15 are deleted. How would you fix this?

I can make it work with Allen's code by changing line 13 to

For J = iStep To iCount Step iStep

2013-06-17 09:59:28

Don usual, thanks for the tip. These get my mind moving in the right direction every week. Below is an alternative approach that selects multiple rows at a time.

Some notes:
* The two constants, plus "ws" and SETting it to ActiveSheet, can be replaced by parameters.
* This uses the worksheet's UsedRange.Rows.Count to identify the last row number, which means the code assumes the whole worksheet is to be processed, which may cause the macro to run a little longer than necessary if the UsedRange.Rows.Count is much greater than the current last row of data.
* An alternative to UsedRange.Rows.Count would be to use the LastWSRow function included below and set that value to a variable that replaces UsedRange.Rows.Count
* If instead of using a Worksheet as a parameter a Range were used, then Range.Rows.Count would be appropriate.

Sub DeleteAlternateRows()
Const StartRow As Double = 2
Const StepSize As Double = 2
Dim ws As Worksheet
Set ws = ActiveSheet

Const RangeStringLimit = 256
Dim RowList() As String
ReDim RowList(1)
Dim i_Rows As Double
Dim i_RL As Double
i_RL = 0

For i_Rows = StartRow To ws.UsedRange.Rows.Count Step StepSize
If Len(RowList(i_RL) & i_Rows & ":" & i_Rows & ",") > RangeStringLimit Then
RowList(i_RL) = Left(RowList(i_RL), Len(RowList(i_RL)) - 1)
ReDim Preserve RowList(i_RL + 1)
i_RL = i_RL + 1
End If
RowList(i_RL) = RowList(i_RL) & i_Rows & ":" & i_Rows & ","
Next i_Rows
RowList(i_RL) = Left(RowList(i_RL), Len(RowList(i_RL)) - 1)

For i_RL = UBound(RowList) To LBound(RowList) Step -1
Next i_RL
End Sub

Function LastWSRow(SearchColumn As Range) As Double

If Len(SearchColumn.Cells(SearchColumn.Cells.count, 1)) > 0 Then
LastWSRow = SearchColumn.Row + SearchColumn.Cells.count - 1
LastWSRow = SearchColumn.Cells(SearchColumn.Cells.count, 1).End(xlUp).Row
End If

End Function

2013-06-17 09:42:15


Using the same paramaters, (i.e., not using any of the very useful changes Duncan suggested), this code should help avoid extra looping, and should speed up the process a bit since it deletes all the rows at once instead of one at a time (this is significant when deleting a lot of rows):

Sub DeleteRows()
Dim iStep As Integer
Dim J As Integer
Dim rngDelete As Range

Application.ScreenUpdating = False

iStep = 2 ' Change this value

Set rngDelete = Selection.Cells(1, 1)

For J = 1 + iStep To Selection.Rows.Count Step iStep
Set rngDelete = Union(rngDelete, Selection.Cells(J, 1))
Next J


End Sub

2013-06-17 09:26:28

Wayne Hamilton

How would you change this macro to adjust the height of every other row (to the same height)?

2013-06-17 06:47:38


With only a little modification this macro could be made to:
- ask for the value of iStep at runtime
- cue user to select a set of example rows to be kept (from which iStep can be derived)
- apply a technique similar to Dave Onorato's but automated.

The plus point on Dave Onorato's approach (once automated) or similar is that "block" operations applied to selected sets of rows tend to be faster than stepping through and acting one step at a time (one row at a time). This becomes more noticeable with larger/longer datasets.

2013-06-15 14:58:05


Thanks, Buddy. Now it works!!

2013-06-15 09:56:18



What you suggest is fine if you only need to do this once or twice. But if you need to do it regularly, like weekly, you will be better off writing a macro once. Going through all the mousing, clicking and typing you suggest can be quite time consuming, especially if you have a large worksheet. I have many repetitive chores that I replaced with macros and am amazed how quickly the macros get it done.

I would encourage everyone to learn some rudimentary VBA programming (not an easy project) and this example nudges folks in this direction.

2013-06-15 08:30:35

Dave Onorato

Why use complicated code? This task is easily done with a sorting technique.
Insert 2 new columns. In A, number the rows sequentially (to assure things going back in the right order). In the B column, enter aaa for the filled line, then bbb for the blank line. Select both cells, and you can autofill this pattern to the end of your data. Then sort by column B. Blanks are at the bottom. Delete them. Delete the B column, and sort by the A column if necessary.
If you need every third row, the pattern in the B column is simply aaa, aaa, bbb. Or if your lines have no pattern, develop an If statement. Then sort on the results.
Anyone can sort. Code is a last resort.

2013-06-15 07:16:25


Error in macro

Do square boxes have meaning?
Should read:
iEnd = iEnd – iStep

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

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.