excel - 在excel 2007中删除A列上的空行

标签 excel vba excel-2007

我有以下代码从 A 列中获取空行,然后删除整行。我无法在 2010 年使用 Special--> Blanks --> Delete Sheet Rows 功能,因为 2007 年的上限约为 8000 个非连续行。这段代码在一些旧机器上非常慢,大约需要 40 分钟才能完成(但可以完成工作)。有没有更快的替代方案?

 Private Sub Del_rows()
    Dim r1 As Range, xlCalc As Long
    Dim i As Long, j As Long, arrShts As Variant
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    arrShts = VBA.Array("Sheet1")  'add additional sheets as required
    For i = 0 To UBound(arrShts)
        With Sheets(arrShts(i))
            For j = .UsedRange.Rows.Count To 2 Step -8000
                If j - 7999 < 2 Then
                    .Range("A2:A" & j).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                Else
                    .Range("A" & j, "A" & j - 7999).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                End If
            Next j
        End With
    Next i
    Application.Calculation = xlCalc

最佳答案

拉吉夫,试试这个。这应该很快。

Option Explicit

Sub Sample()
    Dim delrange As Range
    Dim LastRow As Long, i As Long

    With Sheets("Sheet1") '<~~ Change this to the relevant sheetname
        '~~> Get the last Row in Col A
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To LastRow
            If Len(Trim(.Range("A" & i).Value)) = 0 Then
                If delrange Is Nothing Then
                    Set delrange = .Rows(i)
                Else
                    Set delrange = Union(delrange, .Rows(i))
                End If
            End If
        Next i

        If Not delrange Is Nothing Then delrange.Delete
    End With
End Sub

编辑 :

您还可以使用自动过滤器来删除行。这是相当快的。我 没有测试了如此大行的两个示例:) 如果您遇到任何错误,请告诉我。
Option Explicit

Sub Sample()
    Dim lastrow As Long
    Dim Rng As Range

    With Sheets("Sheet1")
        lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Remove any filters
        .AutoFilterMode = False

        With .Range("A1:A" & lastrow)
          .AutoFilter Field:=1, Criteria1:=""
          .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        '~~> Remove any filters
        ActiveSheet.AutoFilterMode = False
    End With
End Sub

高温高压

席德

关于excel - 在excel 2007中删除A列上的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692100/

相关文章:

excel - VBA 不会从内存棒加载文件,但会从计算机加载。为什么?

regex - 特殊字符的 VBA 正则表达式

excel - 使用 VBA 修改动态名称

excel - 运行时错误 '438' 问题使用 VBA 访问 activeX 控件

vba - 登录网站

vsto - Excel VSTO 2007/VS 2010 AddIn 将不再安装。不管怎样,我可以转储 ClickOnce 吗?

java.lang.IllegalStateException : Cannot get a numeric value from a text cell 错误

vba - 将变量值插入带有文本VBA的单元格

database - Excel 可以将自己用作数据库吗?

excel - 在 Excel 中计算组合对