Delete every other row

做~自己de王妃 提交于 2020-01-11 13:00:44

问题


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

回答1:


"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.




回答2:


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



来源:https://stackoverflow.com/questions/31342024/delete-every-other-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!