删除超过60000行的工作表中的细节行

时间:2014-02-14 08:48:34

标签: excel-vba vba excel

我需要删除以下行: - 没有单词“Get”进入A列,例如:如果A1是Configuration Get,我不应该删除;但如果它是nFormat或其他任何东西,我应该删除。 - 对于包含单词“get”的行,我还需要检查C列中的值是否为0,如果不是0,我也应该删除。

我的功能是用于少量行的工作表,但问题是,我真的需要运行大量的数据,比方说60000行。有人能帮助我吗?

我的功能是:

Sub DeleteRows()

   Dim c As Range
   Dim ColumnA
   Dim Lrow As Long
   Dim Lastrow As Long

With Sheets("Sheet1") 'I'm using the Sheet1
.Select

   Set ColumnA = ActiveSheet.UsedRange
   Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

For Lrow = Lastrow To 1 Step -1

Set ColumnA = Cells(Lrow, "A") 'I'm looking just in Column "A" for a Get

       Set c = ColumnA.Find("Get", LookIn:=xlValues)
       If Not c Is Nothing Then
            ' If the cell has a Get, it will look for a 0 in Column "C"
            With .Cells(Lrow, "C")
                If Not IsError(.Value) Then
                    ' If the Value is not 0 the row will be delete.
                    If Not (.Value = 0) Then .EntireRow.Delete
                End If
            End With

        Else
        'If didn't find a "Get", it will delete the row
        ColumnA.EntireRow.Delete

        End If      
Next Lrow

End With

End Sub

2 个答案:

答案 0 :(得分:1)

尝试使用AutoFilter代替

这样的内容

VBA相当于:

  1. 找到第一个空白栏
  2. 在第1行输入=OR(ISERROR(FIND("Get",$A1)),AND(NOT(ISERROR(FIND("Get",$A1))),$C1<>0))并向下复制
  3. 删除和TRUE结果
  4. 清理工作栏
  5. 代码

    Sub KillEm()
        Dim rng1 As Range, rng2 As Range, rng3 As Range
        Set rng1 = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious)
        Set rng2 = Cells.Find("*", , xlValues, , xlByRows, xlPrevious)
        Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(1, rng1.Column))
        Application.ScreenUpdating = False
        With rng3.Offset(0, 1)
            .FormulaR1C1 = "=OR(ISERROR(FIND(""Get"",RC1)),AND(NOT(ISERROR(FIND(""Get"",RC1))),RC3<>0))"
            .AutoFilter Field:=1, Criteria1:="TRUE"
            .Offset(1, 0).Resize(rng3.Rows.Count - 1, 1).EntireRow.Delete
            .EntireColumn.Delete
        End With
        Application.ScreenUpdating = True
    End Sub
    

答案 1 :(得分:0)

我确实喜欢这个,在本例中是sheet7,它起作用了:

Application.ScreenUpdating = False
With Sheet7
 r = 1
 Do While r <= LastRow
  If IsError(.Cells(r, 1)) Then
   .Rows(r).Delete
   LastRow = LastRow - 1
  Else
   If InStr(.Cells(r, 1), "Get") = 0 Then
    .Rows(r).Delete
    LastRow = LastRow - 1
   Else
    r = r + 1
   End If
  End If
 Loop
End With
Application.ScreenUpdating = True