0

Bom dia, Tenho o seguinte codigo o IF retorna erro "tipo incompatíveis", alguem sabe o motivo? estou a tentar validar os valores de linhas com colunas.

Sub DeleteColumnByHeader()

Dim aCell As Range
Dim bCell As Range

For Each aCell In Range("C5:UZ5")
    For Each bCell In Range("A1:A1044")
     If aCell.Value = bCell.Value Then cell.EntireColumn.Delete
    Next bCell
Next aCell

End Sub
1

1 Resposta 1

0

Na verdade o próprio erro já está falando: "Tipos incompatíveis". Isso significa que o programa não consegue comparar os dois valores para verificarem se são iguais e também não consegue converter um valor de um tipo para o tipo do outro para tentar comparar.

Para evitar o erro tente adicionar uma camada de verificação a mais para verificar se os tipos são iguais:

Sub DeleteColumnByHeader()

Dim aCell As Range
Dim bCell As Range

For Each aCell In Range("C5:UZ5")
    For Each bCell In Range("A1:A1044")
       IF VarType(aCell.Value) = VarType(bCell.Value) Then
          If aCell.Value = bCell.Value Then cell.EntireColumn.Delete
       End If
    Next bCell
Next aCell

End Sub

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .