Excel 删除电子表格中行的有效方法

Excel 删除电子表格中行的有效方法,excel,vba,Excel,Vba,有没有比我目前使用的更好的方法来删除excel电子表格中的行 在电子表格上,我运行一个宏,如果特定单元格中的图形为“0”,该宏将删除一行 Public Sub deleteRowsIfZero(colDelete As Integer) Dim r As Long Application.ScreenUpdating = False For r = Cells(Rows.Count, colDelete).End(xlUp).Row To 1 Step -1 If Cells(r, c

有没有比我目前使用的更好的方法来删除excel电子表格中的行

在电子表格上,我运行一个宏,如果特定单元格中的图形为“0”,该宏将删除一行

Public Sub deleteRowsIfZero(colDelete As Integer)
Dim r As Long
Application.ScreenUpdating = False

For r = Cells(Rows.Count, colDelete).End(xlUp).Row To 1 Step -1
    If Cells(r, colDelete).Value = "0" Then Rows(r).Delete
Next r
Application.ScreenUpdating = True
End Sub
这是可行的,但对于700多行的电子表格来说,速度可能相当慢。有没有更有效的方法

提前谢谢你的建议

干杯


Noel

在开始之前关闭屏幕更新和计算,并在结束时恢复这些设置

请注意,每次删除时都会不必要地重新测试行,因为删除操作会将行向上滑动。因此,您可以在每次删除一个小优化时减小r

进一步的优化是一次读取所有测试值。每次从Excel/VBA读/写都有开销。见下例:

Dim r As Long, n As Long
Dim testcells As Variant

n = Cells(Rows.count, rowdelete).End(xlUp).Row
testcells = Range(Cells(n, rowdelete), Cells(1, rowdelete)).Value

For r = n To 1 Step -1
    If testcells(r, 1) = 0 Then
        Rows(r).Delete
    End If
Next r
您还可以尝试一次删除所有内容,以查看哪个更快

Dim r As Long, n As Long
Dim testcells As Variant
Dim del As Range

n = Cells(Rows.Count, rowdelete).End(xlUp).Row
testcells = Range(Cells(n, rowdelete), Cells(1, rowdelete)).Value

For r = n To 1 Step -1
    If testcells(r, 1) = 0 Then
        If del Is Nothing Then Set del = Rows(r)
        Set del = Union(del, Rows(r))
    End If
Next r

If Not (del Is Nothing) Then
    del.Delete
End If

在开始之前关闭屏幕更新和计算,并在结束时恢复这些设置

请注意,每次删除时都会不必要地重新测试行,因为删除操作会将行向上滑动。因此,您可以在每次删除一个小优化时减小r

进一步的优化是一次读取所有测试值。每次从Excel/VBA读/写都有开销。见下例:

Dim r As Long, n As Long
Dim testcells As Variant

n = Cells(Rows.count, rowdelete).End(xlUp).Row
testcells = Range(Cells(n, rowdelete), Cells(1, rowdelete)).Value

For r = n To 1 Step -1
    If testcells(r, 1) = 0 Then
        Rows(r).Delete
    End If
Next r
您还可以尝试一次删除所有内容,以查看哪个更快

Dim r As Long, n As Long
Dim testcells As Variant
Dim del As Range

n = Cells(Rows.Count, rowdelete).End(xlUp).Row
testcells = Range(Cells(n, rowdelete), Cells(1, rowdelete)).Value

For r = n To 1 Step -1
    If testcells(r, 1) = 0 Then
        If del Is Nothing Then Set del = Rows(r)
        Set del = Union(del, Rows(r))
    End If
Next r

If Not (del Is Nothing) Then
    del.Delete
End If

如果预期的错误很少,则应该更快地找到匹配的单元格并将其删除。列D用于保存要删除的字符

Sub DeleteRowWithCertainValue()

Dim del_char As String

 del_char = "0"

Do
  On Error GoTo ErrorHandler
     Sheets("Sheet1").Range("D:D").Find(What:=del_char, _
        After:=Range("D1"), _
          LookIn:=xlValues, _
            LookAt:=xlWhole, _
           SearchOrder:=xlByColumns, _
          SearchDirection:=xlNext, _
        MatchCase:=False).EntireRow.Delete
Loop

ErrorHandler: Exit Sub

End Sub

如果预期的错误很少,则应该更快地找到匹配的单元格并将其删除。列D用于保存要删除的字符

Sub DeleteRowWithCertainValue()

Dim del_char As String

 del_char = "0"

Do
  On Error GoTo ErrorHandler
     Sheets("Sheet1").Range("D:D").Find(What:=del_char, _
        After:=Range("D1"), _
          LookIn:=xlValues, _
            LookAt:=xlWhole, _
           SearchOrder:=xlByColumns, _
          SearchDirection:=xlNext, _
        MatchCase:=False).EntireRow.Delete
Loop

ErrorHandler: Exit Sub

End Sub

我会使用一个连续的范围,一次删除它:

Public Sub deleteRowsIfZero(strSeekValue As String)
Dim lngRowsToDelete() As Long
Dim strRowsToDelete() As String
Dim x As Long, y As Long, n As Long, z As Long

On Error GoTo err_

'get the extent of the workbook range
x = Me.UsedRange.Rows.Count

n = -1

'switch off screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'starting from row 1, look for the strSeekValue in column A, keep going till x is reached
For y = 1 To x
    If Me.Range("A" & y).Value = strSeekValue Then 'if we find one, pop the row number into the array
        n = n + 1
        ReDim Preserve lngRowsToDelete(0 To n)
        lngRowsToDelete(n) = y
    End If
Next y

'if none were found, don't do the next bit
If n = -1 Then GoTo err_

'create a string of all the rows we found
z = 0
ReDim strRowsToDelete(z)
For y = 0 To n
    strRowsToDelete(z) = strRowsToDelete(z) & lngRowsToDelete(y) & ":" & lngRowsToDelete(y) & ","
    If Len(strRowsToDelete(z)) > 240 Then 'As A.Webb points out, the 255 limit will be a problem here
        strRowsToDelete(z) = Left(strRowsToDelete(z), Len(strRowsToDelete(z)) - 1) 'drop the trailing comma
        z = UBound(strRowsToDelete) + 1 'resize the array
        ReDim Preserve strRowsToDelete(0 To z)
    End If
Next y

For y = z To 0 Step -1
    If Right(strRowsToDelete(z), 1) = "," Then strRowsToDelete(z) = Left(strRowsToDelete(z), Len(strRowsToDelete(z)) - 1)
    'now delete the rows
    Me.Range(strRowsToDelete(y)).EntireRow.Delete
Next y

err_:
'assumes calculation was set to auto
Application.Calculation = xlCalculationAutomatic
If Err Then Debug.Print Err.Description
Application.ScreenUpdating = True
End Sub


'run sub foo
Sub foo()
deleteRowsIfZero "0"
End Sub

我会使用一个连续的范围,一次删除它:

Public Sub deleteRowsIfZero(strSeekValue As String)
Dim lngRowsToDelete() As Long
Dim strRowsToDelete() As String
Dim x As Long, y As Long, n As Long, z As Long

On Error GoTo err_

'get the extent of the workbook range
x = Me.UsedRange.Rows.Count

n = -1

'switch off screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'starting from row 1, look for the strSeekValue in column A, keep going till x is reached
For y = 1 To x
    If Me.Range("A" & y).Value = strSeekValue Then 'if we find one, pop the row number into the array
        n = n + 1
        ReDim Preserve lngRowsToDelete(0 To n)
        lngRowsToDelete(n) = y
    End If
Next y

'if none were found, don't do the next bit
If n = -1 Then GoTo err_

'create a string of all the rows we found
z = 0
ReDim strRowsToDelete(z)
For y = 0 To n
    strRowsToDelete(z) = strRowsToDelete(z) & lngRowsToDelete(y) & ":" & lngRowsToDelete(y) & ","
    If Len(strRowsToDelete(z)) > 240 Then 'As A.Webb points out, the 255 limit will be a problem here
        strRowsToDelete(z) = Left(strRowsToDelete(z), Len(strRowsToDelete(z)) - 1) 'drop the trailing comma
        z = UBound(strRowsToDelete) + 1 'resize the array
        ReDim Preserve strRowsToDelete(0 To z)
    End If
Next y

For y = z To 0 Step -1
    If Right(strRowsToDelete(z), 1) = "," Then strRowsToDelete(z) = Left(strRowsToDelete(z), Len(strRowsToDelete(z)) - 1)
    'now delete the rows
    Me.Range(strRowsToDelete(y)).EntireRow.Delete
Next y

err_:
'assumes calculation was set to auto
Application.Calculation = xlCalculationAutomatic
If Err Then Debug.Print Err.Description
Application.ScreenUpdating = True
End Sub


'run sub foo
Sub foo()
deleteRowsIfZero "0"
End Sub


您正在使用Application.ScreenUpdate=False吗?@JamieBull是的,我正在使用Application.ScreenUpdate=False,已修改了我的问题。rowDelete应为Coldelete您正在使用Application.ScreenUpdate=False吗?@JamieBull是的,我正在使用Application.ScreenUpdate=False,已修改我的问题。应删除行。已修改问题,因为我已关闭屏幕更新。我们也关闭了计算功能,看起来可能有一些改进。在速度较快的机器上,这不是一个主要问题,但在速度较慢的机器上,循环需要一段时间。@noelmcg添加了一些其他内容供您尝试。@noelmcg和另一个。。。试试这些和Jüri Ruut的解决方案,让我们知道哪个最适合您!干杯@A.Webb,我收到一个错误91对象变量或del.Delete行上未设置变量。除此之外,它的岩石!我添加了以下内容,如果不是del,那么del.Delete结束如果有效,那么我已经修改了这个问题,因为我关闭了屏幕更新。我们也关闭了计算功能,看起来可能有一些改进。在速度较快的机器上,这不是一个主要问题,但在速度较慢的机器上,循环需要一段时间。@noelmcg添加了一些其他内容供您尝试。@noelmcg和另一个。。。试试这些和Jüri Ruut的解决方案,让我们知道哪个最适合您!干杯@A.Webb,我收到一个错误91对象变量或del.Delete行上未设置变量。除此之外,它的岩石!我添加了下面的If Not del Is Nothing Then del.Delete End If work a treaty我想你会发现,当strorwstodelete超过255个字符时,这可能会出现在43个2位数的行号中,例如,这段代码在RangeStrowstodelete处会失败。@Zilpher我已经尝试过这一点,并做了一些修改以满足我的需要,它绝对是拉链式的@Zipher这很有效,但我将采用A.Webb的解决方案。感谢您向我展示了一种不同的技术。我想您会发现,当strRowsToDelete超过255个字符时(例如,只有43个2位数的行号会出现这种情况),该代码在RangeStrowstodelete中会失败。@Zilpher我已经尝试过这种方法,并且为了满足我的需要做了一些小的修改,它绝对可以通过它@Zipher这很有效,但我将采用A.Webb的解决方案。谢谢你给我展示了一种不同的技术。谢谢你给我介绍了一种不同的技术,但我将使用a.Webbs解决方案。我为未来做了一些工作:-顺便说一句,获得时间来完成所有代码变体的任务是很有趣的。@noelmcg过程所花费的时间:感谢您对不同技术的指导,但我将使用a.Webbs解决方案。我为未来做了一些工作:-顺便说一句,获取完成所有代码变体任务的时间会很有趣。@noelmcg过程所花费的时间: