2

什么会导致这条线

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
4

1 回答 1

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。如果值为i17,则检查单元格 C17。我确信该单元格中有一个给出错误的公式。例如#DIV/0!或任何其他错误。只有在这种情况下,它才会给出一个type mismatch error.

快照

在此处输入图像描述

于 2012-05-03T17:10:23.487 回答