删除包含数据的行:缓冲区问题

时间:2015-04-09 13:22:03

标签: excel vba excel-vba

我需要在大约10000行中删除C列中包含“$”的所有行。我试过这个,但需要永远完成。有人有更快或更有效的方法吗?

Last = Cells(Rows.Count, "C").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "C").Value) = "$" Then
            Cells(i, "A").EntireRow.Delete
    End If
Next i

2 个答案:

答案 0 :(得分:1)

使用.Find()

尝试
Dim rng As Range
'Application.ScreenUpdateing = False  'don't enable this until you're sure the 
   'works correctly
Set rng = Range.Columns("C").Find(what:="$", LookIn:=xlValues)
While Not rng Is Nothing
  rng.EntireRow.Delete
  Set rng = Range.Columns("C").Find(what:="$", LookIn:=xlValues)
Wend
Set rng = Nothing
Application.ScreenUpdateing = False

您可能希望在.Find()调用中添加一些额外参数,以准确指定您要查找的内容 - 它使用的参数与Find中设置的参数完全相同对话框,除非您在代码中覆盖它们,并在下次打开Find对话框时反映在代码中设置它们,因此您必须非常小心。

答案 1 :(得分:0)

您可以在操作时停用一些无用的功能,我添加了一个Timer,以便您进行比较。

第一个选项,与您删除的操作相同且没有数组:

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim TimingT As Long
TimingT = Time

last = Cells(Rows.Count, "C").End(xlUp).Row
For i = last To 1 Step -1
    If (Cells(i, "C").Value) = "$" Then
            Cells(i, "A").EntireRow.Delete
    End If
Next i

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

MsgBox "Total duration of operation : " & Format(Time - TimingT, "hh:mm:ss"), vbInformation, "Procedure finished"

第二个选项,带有数组:

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim LastR As Long
Dim TimingT As Long
TimingT = Time
Dim ToDel()

LastR = Cells(Rows.Count, "C").End(xlUp).Row
ReDim ToDel(LastR)

For i = 1 To UBound(ToDel)
    ToDel(i) = InStr(1, Cells(i, "C"), "$")
Next i

For i = UBound(ToDel) To 1 Step -1
    If ToDel(i) <> 0 Then
        Rows(i).EntireRow.Delete
    End If
Next i

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

MsgBox "Total duration of operation : " & Format(Time - TimingT, "hh:mm:ss"), vbInformation, "Procedure finished"

第3个选项,clearcontents然后排序(没有做但可能有趣......)