Home » excel » excel – VBA Macro: Error 13 Type Mismatch, error 2023 on cell

excel – VBA Macro: Error 13 Type Mismatch, error 2023 on cell

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to make a macro that groups up rows and sums the amount of items that they have on a column, I made it so it works but after a few rows it just crashes and displays

Error 13: Type mismatch.

When I open the code it highlights this line

stringb = Cells(cont2, 7)

The code is this one:

Sub Juntarfilas()
    Dim Av1 As Double
    Dim cont As Long
    Dim cont2 As Long
    Dim numcol As Integer
    Dim comp1 As Integer
    Dim check As String
    Dim string1 As String
    Dim string2 As String
    Dim stringa As String
    Dim stringb As String
    Dim Rango As Variant

    Sheets("RESULTADO").Select
    numcol = Range("E2").Column
    cont = 2
    cont2 = 3

    If IsEmpty(Range("E2").Value) = True Then
        check = 0
    Else
        check = 1
    End If

    While (check = 1)
        string1 = Cells(cont, 5)
        string2 = Cells(cont2, 5)
        stringa = Cells(cont, 7)
        stringb = Cells(cont2, 7)
        comp1 = StrComp(string1, string2)
        If (comp1 = "0") Then
            Rango = Range(Cells(cont, 7), Cells(cont2, 7))
            Av1 = Application.WorksheetFunction.Sum(Rango)
            Cells(cont, 7) = Av1
            cont = cont
            cont2 = cont2
            Rows(cont2).EntireRow.Delete
        Else
            cont = cont + 1
            cont2 = cont2 + 1
        End If

        If IsEmpty(string1) = True Then
            check = 0
        Else
            check = 1
        End If
    Wend
End Sub

I can’t come up with a solution and I came here to see if someone could help me, thanks in advance.

If it’s necessary I can send the excel file too.

How to&Answers:

The Type Mismatch error occurs when the cell in stringb = Cells(cont2, 7) contains a formula that returns an error (eg #DIV/0! or #N/A! or any other).

Use stringb = Cells(cont2, 7).Text instead to get the error text as string.