VBA to delete every other line

smugtim

Board Regular
Joined
Mar 7, 2005
Messages
54
Hello,

If somebody has some VBA code written to delete every other line from row x to row y, it would be much appreciated! I'm very new to VBA and don't know how to go about doing this.

Thanks very much,

Tim
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Dim c As Range, rng
Set rng = Range("a11:a35")
For Each c In rng
If c.Row() / 2 > 0 Then
c.EntireRow.Delete
End If
Next c
 
Upvote 0
Delete alternate rows

Hi Tim,

Here is one that prompts you for the range of rows.

Sub DelAlternateRows()
'Deletes every other row in a defined range of rows
'Examples: start row = 10, end row = 20, deletes rows 11, 13, 15, 17, 19
' start row = 1, end row = 10, deletes rows 2, 4, 6, 8, 10
' start row = 0, end row = 10, deletes rows 1, 3, 5, 7, 9
' Prompts for start and end rows.

Dim Rows2Delete As Range
Dim StartRow As Long
Dim EndRow As Long
Dim iRow As Long

StartRow = Application.InputBox("Enter Start Row", "Delete Alternate Rows", _
1, Type:=1)
With ActiveSheet.UsedRange
EndRow = .Row + .Rows.Count - 1
End With
EndRow = Application.InputBox("Enter End Row", "Delete Alternate Rows", _
EndRow, Type:=1)
iRow = StartRow + 1
Set Rows2Delete = Rows(iRow)
iRow = iRow + 2

Do While iRow <= EndRow
Set Rows2Delete = Union(Rows2Delete, Rows(iRow))
iRow = iRow + 2
Loop

Rows2Delete.Delete

End Sub


Damon
 
Upvote 0
Damon,
how do i modify your macro to change it to delete every 2 row ? Do i need to input another set of commands i.e. range and select range ?
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top