0

正如您在下面看到的,当列 F 中的任何单元格的值为零时,我试图删除整个。但是,我的代码在第 3 行不断调试。

' DELETES ALL ROWS FROM F2 DOWNWARDS WITH THE VALUE = 0 IN COLUMN F

Last = Cells(Rows.Count, 6).End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, 6).Value) = 0 Then
        Cells(i, 6).EntireRow.Delete
    End If
Next i
4

1 回答 1

0

你的代码看起来不错。如果 F 列中的一个单元格返回错误(例如,数字除以零),我能够复制您得到的错误。当您的代码询问ActiveSheet.Cells(i, 6).Value = 0以下代码是否在循环单元格时忽略错误但会告诉您返回此错误的单元格时,会发生这种情况。

Sub Test()
    Dim Last As Double
    Dim i As Double

    Last = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row

    On Error Resume Next
        For i = Last To 1 Step -1
            If ActiveSheet.Cells(i, 6).Value = 0 Then
                If Err > 0 Then
                    MsgBox "There is an error in cell " & ActiveSheet.Cells(i, 6).Address
                    Err.Clear
                Else
                    ActiveSheet.Cells(i, 6).EntireRow.Delete
                End If
            End If
        Next i
    Err.Clear
    On Error GoTo 0
End Sub
于 2013-10-31T19:07:49.583 回答