删除海量数据集中的空行

时间:2019-02-01 18:00:20

标签: excel vba

我大约有30000行,仅包含8000条记录。我需要这些记录之间删除空行(22000)。

此代码太慢了。

Do While True
    If IsEmpty(Cells(j, 1)) Then
        Rows(j).Delete
    ElseIf True Then
        j = j + 1
    End If
Loop

1 个答案:

答案 0 :(得分:2)

我建议不要编写VBA来解决此问题,而应为数据添加过滤器,过滤到空白行,删除它们,然后清除过滤器。如果您坚持使用VBA进行此操作,则需要进行以下更改:

  1. 向后迭代数据(从工作表的底部到顶部)。这是因为在删除行时,行号会移动。
  2. 关闭屏幕更新和计算。这将产生巨大的变化。

下面是代码示例:

Sub RemoveBlankRows()

    'Add performance improvements
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    'Get last row of worksheet that contains data (based on column 1)
    iLastRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Iterate through rows
    For i = iLastRow To 1 Step -1
        If IsEmpty(Cells(i, 1)) Then
            Rows(i).Delete
        End If
    Next i

    'Remove Performance Optimizations
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub
相关问题