Excel VBA - Run-time error '13': Incompatible Types

1

I'm getting the error '13' Incompatible types, in the line of the first If:

Count = 0
For x = 1 To LastRow Step 1
    If Cells(x, 8).Value = "Materiais" Or Cells(x, 8).Value = "Imobilizado" Then
        If Cells(x, 11).Value = "NÃO CATALOGO" Then
            Count = Count + 1
        End If
    End If
Next x

Would anyone know where the problem is?

    
asked by anonymous 11.05.2018 / 19:49

2 answers

1

Place .Text in place of .Value in tests made with if.

Count = 0
For x = 1 To LastRow
    If Cells(x, 8).Text = "Materiais" Or Cells(x, 8).Text = "Imobilizado" Then
        If Cells(x, 11).Text= "NÃO CATALOGO" Then Count = Count + 1
    End If
Next x

Another thing is that this code depends on having the desired sheet active. If you do not want it to be active, use a variable to reference it. Ex.

    set ws = ThisWorkbook.Sheets("SheetName")

    Count = 0
    For x = 1 To LastRow
        If ws.Cells(x, 8).Text = "Materiais" Or ws.Cells(x, 8).Text = "Imobilizado" Then
            If ws.Cells(x, 11).Text= "NÃO CATALOGO" Then Count = Count + 1
        End If
    Next x
    
06.08.2018 / 17:21
0

I've tested your code and nothing abnormal, here's what I did. Apparently it's working properly. Have you tried to review logic, what expected result?

Tickets

Code

Subtst()LastRow=18Count=0Forx=1ToLastRowStep1IfCells(x,8).Value="Materiais" Or Cells(x, 8).Value = "Imobilizado" Then
            Debug.Print Cells(x, 8).Value & vbTab & " - " & vbTab & Count
            If Cells(x, 11).Value = "NÃO CATALOGO" Then
                Count = Count + 1
            End If
        End If
    Next x
End Sub

Results:

Materiais    -  0
Materiais    -  0
Imobilizado  -  0
Materiais    -  0
Materiais    -  1
Materiais    -  1
Imobilizado  -  1
Materiais    -  1
Materiais    -  2
Imobilizado  -  2
Materiais    -  2
Materiais    -  3
Imobilizado  -  3
Imobilizado  -  3
Imobilizado  -  3
Imobilizado  -  3
Imobilizado  -  4
Imobilizado  -  4
    
06.08.2018 / 17:15