简体   繁体   English

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

[英]Delete Rows containing data: buffer issues

I need to delete all rows containing "$" in Column C on approximately 10000 rows. 我需要删除大约10000行中列C中所有包含“ $”的行。 I've tried this but it takes forever to complete. 我已经尝试过了,但是要花很长时间才能完成。 Someone has a quicker or more efficient way of doing this? 有人有更快或更有效的方法吗?

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

Try it using .Find() 使用.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

You'll probably want to add a couple of extra parameters to the .Find() call to specify exactly what you're looking for - it uses the parameters exactly as they're set in the Find dialog box unless you override them in code, and setting them in code is reflected the next time you open the Find dialog box, so you have to be very careful. 您可能需要向.Find()调用中添加几个额外的参数,以确切指定您要查找的内容-除非您在代码中覆盖了这些参数,否则它将使用与“ Find对话框中设置的参数完全相同的参数,并在下次打开“ Find对话框时反映在代码中进行设置,因此必须非常小心。

You can deactivate some useless features while you are operating, I added a Timer so that you can compare. 您可以在操作时停用一些无用的功能,我添加了一个计时器,以便您进行比较。

1st option, same action as you delete and no array : 第一个选项,与删除操作相同,没有数组:

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"

2nd option, with an array : 第二个选项,带有数组:

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"

3rd option, clearcontents and then sort (didn't do it but could be interesting...) 第三种选择,先清除内容,然后进行排序(虽然没有做,但是可能很有趣...)

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

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