Brian
Brian

Reputation: 29

Delete every other row

I want to write a loop that deletes entire rows in a sheet to clean-up the file. So if I have row 1 to 10, I want to delete row 1, 3, 5, 7, 9.

Sub deleteeveryotherrow()
 For x = 1 To 10

     Rows("x:x").Select
        Selection.Delete Shift:=xlUp
        x = x + 2
        Next x

    End Sub

Upvotes: 0

Views: 3369

Answers (2)

Bathsheba
Bathsheba

Reputation: 234645

"x:x" is just a literal string, so will not work as you expect.

Use Rows(x & ":" & x).Select instead.

You should also consider running the loop backwards from 10 to 1:

For x = 9 To 1 Step -2

otherwise you'll get your indexing tangled up. You'll then be able to remove the line x = x + 2.

Upvotes: 2

Brian
Brian

Reputation: 29

Sub deleteeveryotherrow()

For x = 1 To 100 Step 1

 Rows(x & ":" & x).Select
    Selection.Delete Shift:=xlUp

    Next x

End Sub

this works perfectly thanks

Upvotes: 0

Related Questions