需要VBA帮助:使用此删除行代码获取调试消息

时间:2013-10-31 18:58:29

标签: excel vba

如下所示,当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

1 个答案:

答案 0 :(得分: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