问题
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