Excel:运行时错误'13'在删除行执行期间键入不匹配

时间:2012-05-03 16:51:08

标签: excel vba excel-vba

什么会导致这一行

If Cells(i, 3) = "" Or VBA.Left(Cells(i, 3), 5) = "BIGA-" Or VBA.Left(Cells(i, 3), 5) = "BRNG-" Or VBA.Left(Cells(i, 3), 5) = "ENER-" Or VBA.Left(Cells(i, 3), 5) = "EURE-" Or VBA.Left(Cells(i, 3), 5) = "STRE-" Then Rows(i).Delete

在下面的宏中,在找到并删除第一个单元格记录后返回运行时错误“13”类型不匹配?

Option Explicit

Sub deletedExceptions_row()
Dim i As Long
For i = Cells.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
    If Cells(i, 3) = "" Or 
    VBA.Left(Cells(i, 3), 5) = "BIGA-" Or 
    VBA.Left(Cells(i, 3), 5) = "BRNG-" Or 
    VBA.Left(Cells(i, 3), 5) = "ENER-" Or 
    VBA.Left(Cells(i, 3), 5) = "EURE-" Or 
    VBA.Left(Cells(i, 3), 5) = "STRE-" Then
    Rows(i).Delete
Next i
End Sub

1 个答案:

答案 0 :(得分:3)

试试这个

Option Explicit

Sub deletedExceptions_row()
    Dim i As Long
    Dim ws As Worksheet

    On Error GoTo whoa

    Set ws = Sheets("Sheet1")

    With ws
        For i = .Cells.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
            If .Cells(i, 3) = "" Or _
            VBA.Left(.Cells(i, 3), 5) = "BIGA-" Or _
            VBA.Left(.Cells(i, 3), 5) = "BRNG-" Or _
            VBA.Left(.Cells(i, 3), 5) = "ENER-" Or _
            VBA.Left(.Cells(i, 3), 5) = "EURE-" Or _
            VBA.Left(.Cells(i, 3), 5) = "STRE-" Then
                .Rows(i).Delete
            End If
        Next i
    End With
    Exit Sub
whoa:
    MsgBox "Value of i is " & i, vbInformation, Err.Description
End Sub

现在是否会出错?如果确实如此,那么i的值是多少。如果i的值为17,则检查单元格C17。我确信那个单元格中有一个公式给出错误。例如#DIV/0!或任何其他错误。只有在这种情况下它才会给出type mismatch error

SNAPSHOT

enter image description here