简体   繁体   English

删除excel 2007中A列的空行

[英]Delete empty rows on Column A in excel 2007

I have the following piece of code that takes in the empty rows from Column A and then deletes the entire row.我有以下一段代码,它接收 A 列中的空行,然后删除整行。 I could not use the Special--> Blanks --> Delete Sheet Rows feature on 2010 because 2007 has an upper bound of approximately 8000 non contiguous rows.我无法在 2010 年使用 Special--> Blanks --> Delete Sheet Rows 功能,因为 2007 年有大约 8000 个非连续行的上限。 This code is super slow on some older machines and takes about 40 minutes to finish (but does the job).这段代码在一些较旧的机器上非常慢,需要大约 40 分钟才能完成(但可以完成工作)。 Is there any faster alternative to this?有没有更快的替代方法?

 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

Rajiv, Try this.拉吉夫,试试这个。 This should be fast.这应该很快。

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

EDIT :编辑

You can also use Autofilter to delete the rows.您还可以使用自动筛选来删除行。 This is pretty fast.这是相当快的。 I haven't tested both the examples for such huge rows :) Do let me know if you get any errors.还没有测试过如此大行的两个示例:) 如果您遇到任何错误,请告诉我。

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

HTH HTH

Sid锡德

This code takes under a second on 100,000 rows (record the actions for the fuller code if needed):此代码在 100,000 行上花费不到一秒钟的时间(如果需要,记录更完整代码的操作):

Sub DeleteRows()

Application.ScreenUpdating = False
Columns(1).Insert xlToRight
Columns(1).FillLeft
Columns(1).Replace "*", 1
Cells.Sort Cells(1, 1)
Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Columns(1).Delete

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM