capnhud
capnhud

Reputation: 465

Excel: Run-Time Error '13' Type Mismatch during a delete row execution

What would cause this line

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

in the below macro to return a Run-Time Error '13' Type Mismatch after the first cell record is found and deleted?

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

Upvotes: 2

Views: 2944

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149335

Try this

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

Now does it give an error? If it does then what is the value of i. If the value of i is say 17 then check the cell C17. I am sure there is a formula in that cell which is giving an error. For example #DIV/0! or any other error. It is only in this scenario will it give a type mismatch error.

SNAPSHOT

enter image description here

Upvotes: 3

Related Questions