VBA:使用变量删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42673218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:15:40  来源:igfitidea点击:

VBA : Delete rows using variables

vbaexcel-vbaruntime-errorexcel-2007excel

提问by Francis

I wanted to delete rows from 2 to z (z > 2). I wrote 2 methods but neither worked. I searched for answers but didn't find a solution.

我想删除从 2 到 z (z > 2) 的行。我写了 2 种方法,但都没有奏效。我搜索了答案,但没有找到解决方案。

'.Rows("2:" & z).Select  
.Range(Cells(2, 1), Cells(z, 10)).Select  
Selection.Delete Shift:=xlUp

Thank you in advance.

先感谢您。

回答by R3uK

You need to specify you want to select the rows with EntireRow, as your range is only a few cells :

您需要指定要选择的行EntireRow,因为您的范围只有几个单元格:

.Range(.Cells(2, 1), .Cells(z, 10)).EntireRow.Delete Shift:=xlUp


Or cleaner ways if you directly use Rows:

如果您直接使用,或者更清洁的方法Rows

.Range(.Rows(2), .Rows(z)).Delete Shift:=xlUp

Or

或者

.Rows("2:" & z).Delete Shift:=xlUp

回答by Ahmed AbdelKhalek

You can use this simple one line to delete any number of rows you need.

您可以使用这个简单的一行来删除您需要的任意数量的行。

Rows("2:" & Z).Delete