0

我有一个 Excel,我想根据特定的单元格值删除一些行,但是 Excel 非常非常大,它有大约 75000 行。我尝试编写一个普通函数来遍历工作表,检查单元格值并删除匹配的行,但这需要很长时间。我等了 30 分钟,程序仍在运行,有人可以建议我如何实现这一点。

以下是代码。

Private Sub CommandButton1_Click()
    Dim i As Integer

        For i = Range("D1").End(xlDown).Row To 1 Step -1
                If Cells(i, 4) = 7 Then
                    Rows(i).Delete Shift:=xlUp
                End If
        Next i
End Sub

我为 50 行的小型 Excel 文件测试了这段代码,它工作正常。但是当我尝试使用 excel 时,我想尝试它让我等待了 30 分钟并且无法完成。

4

1 回答 1

1

If you want to delete rows in which column D has the value 7 and there are no blanks in column D, then run:

Sub Tachyon()
    Dim rD As Range
    Set rD = Intersect(Range("D:D"), ActiveSheet.UsedRange)
    rD.Replace "7", ""
    rD.Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

EDIT1:

DO NOT USE THIS MACRO

Testing indicates that not only does it remove rows containing 7 in column D, it also removes rows whose cells containing 77 or 777. It also corrupts values like 17 or 71. We need something better.

EDIT2

This version has the problem fixed:

Sub Tachyon2()
    Dim rD As Range
    Set rD = Intersect(Range("D:D"), ActiveSheet.UsedRange)
    rD.Replace What:="7", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    rD.Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
于 2013-09-11T00:32:05.590 回答