如何加快删除行脚本?
我有一个有200,000多行的电子表格。 我需要通过它,如果在某一列,一个单元格是空白的,然后删除该行。
我想知道是否有更快的方法或任何想法如何加快这一点。
这是我在循环删除行的地方:
For i = Cells(Rows.Count, LastRowCounter).End(xlUp).row To headerQ Step -1 If IsEmpty(Cells(i, Column2DeleteRowsFrom).Value) Then Cells(i,Column2DeleteRowsFrom).EntireRow.Delete Next i
注意:“lastRowCounter”是我select的列(即“A”,“B”等)。“HeaderQ”是1或2,具体取决于我是否有标题。
AFAIK主要的另一种方式是使用,而不是for循环,我有,做一些像(伪代码)
For each cel in Range([the range]) If isempty(cel) then delete next cel
但不知道那会更快。
感谢您的任何想法/提示!
(注意:我closures了屏幕刷新function,在表单中也没有计算,只是数据)。
使用SpecialCells
方法一次select所有相关的单元格,并删除整个行:
Sub delemtpy() Dim testcol As Range Dim lastRow As Long Dim headerQ As Long Dim Column2DeleteRowsFrom As Long Dim LastRowCounter As Long lastRow = Cells(Rows.Count, LastRowCounter).End(xlUp).Row Set testcol = Range(Cells(headerQ, Column2DeleteRowsFrom), Cells(lastRow, Column2DeleteRowsFrom)) On Error Resume Next ' if no empty cells present testcol.SpecialCells(xlCellTypeBlanks).EntireRow.Delete On Error GoTo 0 End Sub
这将处理search列根本不包含空单元格的情况。 请注意使用LastRowCounter
来确定使用的范围。
如果您使用Excel 2010或更高版本,请使用SpecialCells …
Range(Cells(headerQ, Column2DeleteRowsFrom), Cells(Rows.Count, Cells(Rows.Count, LastRowCounter).End(xlUp).Row)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete