vba 使用基于数字数组的VBA删除excel中的多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12716111/
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
Delete multiple rows in excel using VBA based on an array of numbers
提问by user1490083
I populate an array of numbers with some criteria and then what I am trying to get to is deleted all of the rows that are in this area.
我用一些条件填充了一个数字数组,然后我想要得到的是删除该区域中的所有行。
Basically I go through a column and if in that specific row, the cell in this column matches a criteria, I add that row number into an array. After it is done going through all rows I want to delete all of the row numbers.
基本上,我遍历一列,如果在该特定行中,该列中的单元格符合条件,则将该行号添加到数组中。完成所有行后,我想删除所有行号。
I'm having trouble figuring out how to delete all rows at once because obviously if I do it one at a time the row numbers change as the one prior or below gets deleted. Because of this I want to select all of the rows together and then just call the Delete command on all rows at once. ANy ideas?
我在弄清楚如何一次删除所有行时遇到了麻烦,因为显然如果我一次删除一个行,那么行号会随着前一行或后一行被删除而改变。因此,我想一起选择所有行,然后一次对所有行调用 Delete 命令。有任何想法吗?
回答by Tim Williams
Sub Tester()
Dim arr
arr = Array(3, 5, 7, 9)
ActiveSheet.Range("A" & Join(arr, ",A")).EntireRow.Delete
End Sub
回答by enderland
Iterate backwards through your rows.
向后遍历您的行。
Something like:
就像是:
Sub tester()
'setting ScreenUpdating false makes this go faster...
Application.ScreenUpdating = False
Dim i As Integer
'go through all rows starting at last row
For i = Range("A1:E5").Rows.Count To 1 Step -1
'check if you need to delete them (you will want to update this)
If Cells(i, 1).Value = "Delete this row!" Then
Rows(i).Delete
End If
Next i
Application.ScreenUpdating = True
End Sub
回答by cyrus
Here's a simple one:
这是一个简单的:
If Range("B1") <> "" Then ' Range that bears the array of cell.addresses....
ar = Array(Range(Range("B1").Cells))
For Each a In ar
a.EntireRow.ClearContents
Next
Range("B1").ClearContents
End If