删除每隔一行 [英] Delete every other row

查看:80
本文介绍了删除每隔一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个循环,该循环删除工作表中的所有行以清理文件.因此,如果我有第1至10行,我想删除第1、3、5、7、9行.

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

推荐答案

"x:x"只是一个文字字符串,因此无法按预期工作.

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

改为使用Rows(x & ":" & x).Select.

您还应该考虑将循环从10倒向1:

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

For x = 9 To 1 Step -2

否则,您的索引将被弄乱.然后,您将可以删除x = x + 2行.

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

这篇关于删除每隔一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆