使用Autofter在该行中使用多个空白单元格删除行



我们有一个超过550,000行的Excel文件,我的任务是"清理"此工作表。该文件包含我们从不同供应商那里获得的所有产品以及随之而来的所有数据。

我遇到的特定任务是如何删除A和B列的所有行均为空的所有行。

使用较小的Excel文件,可以很容易地使用以下方式完成:

Dim j As Long, i As Long
j = Cells(Rows.Count, "C").End(xlUp).Row
For i = j To 1 Step -1
    If Cells(i, "A") = "" And Cells(i, "B") = "" Then
        Cells(i, "A").EntireRow.Delete
    End If
Next I

不幸的是,由于文件的庞大大小,这无效。

在此之前,我成功地删除了使用以下代码中C列中具有空白单元格的所有行:

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim oldWs As Worksheet, newWs As Worksheet
Dim wsName As String, lastRow As Range
Set oldWs = Worksheets(2)
wsName = oldWs.Name
lastRow = ActiveSheet.UsedRange.Rows.Count
If lastRow.Rows.Count > 1 Then
    Set newWs = Sheets.Add(After:=oldWs)
    With lastRow
        .AutoFilter Field:=3, Criteria1:="<>"
        .Copy
    End With
    With newWs.Cells
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial xlPasteAll
        .Cells(1, 1).Select
        .Cells(1, 1).Copy
    End With
    oldWs.Deletesheet
    newWs.Name = wsName
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

我一直在四处寻找如何使用。

加速第一个提到的过程的任何方法或方法,允许将多个字段输入.autofilter,或者通常只是任何其他方式,都将是一个很大的帮助

P.S。我几乎查看了所有相关的问题/答案,找不到对我有用的东西,但是我觉得自己接近

这应该帮助您到达"删除A列A和B列均为空的所有行"

Option Explicit
Sub main()
    With Worksheets("Products") ' change "Products" to your actual sheet name
        With .Range("A1", .Cells(.Rows.Count, "C").End(xlUp))
            .AutoFilter Field:=1, Criteria1:="="
            .AutoFilter Field:=2, Criteria1:="="
            MsgBox .Address
            If Application.WorksheetFunction.Subtotal(103, .Columns(3)) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
End Sub

什么是 N?您不会在任何地方使用它

Dim j As Long, i As Long
j = Cells(Rows.Count, "C").End(xlUp).Row
For i = j To 1 Step -1
       If Range("A" & i).Value = vbNullString And Range("B" & i).Value = vbNullString Then Rows(i).Delete
Next i

应该正常工作。请记住,有500,000行需要一段时间才能处理。

您可以通过放置

来加快过程
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With

开始时(最后将所有内容都放回True(

最新更新