Friend I’ll give an example that happened to me. I also had this same problem when writing a code. I had a string that contained a list of materials and used the split function to, from that string, create several substrings and play them in a vector.
material = DAD_CP.Cells(cont, 8).Value 'Pega a lista_material de materiais e joga na string material
If (material <> "") Then
lista_material = Split(material, Chr(10)) 'Coloca todos os materiais no Array lista_material
'lista_material(UBound(lista_material) + 1) = Mid(material, (InStr(InStr(1, Chr(10), material) + 1, Chr(10), material) + 1), Len(material)) ' Pega o último item da lista_material de materiais
End If
I had declared list material and work list in the same line, and always gave this Incompatible Types error. What gave me anger is that the types were compatible yes because the material variable is a string and the variable lista_material() was a vector of the type string. But then I decided to display the variable window while I Purged the code and ended up discovering that disgraceful was not as a string vector but rather a vector type Variant, even if the code was declared as String. Can you believe it? Then I declared her String only in a separate and isolated line, and guess what? It worked! Conclusion: It seems that VBA does not recognize when vc declares two vectors of the Type string in the same line because lista_obra was as string vector but material list not.
See above that when I declare the two vectors on the same line one stays as a string and the other stays as Variant. But now see that when I declare both separately it stays with its original types. I think this must be some VBA Excel bug. But to conclude the reasoning, when this annoying error of incompatible types occurs try to declare the variables separately.
Try to use
CStr(Cells(x, 8).Value)
to convert the variable to String.– danieltakeshi
Another question: Does your workbook have multiple worksheets? Sometimes the executed code is taking the values of another spreadsheet and not the desired one.
– danieltakeshi