Type mismatch error when using range

Copper Contributor

Hello,

 

Putting together bits and pieces of code found online,

 

This code works without error:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("F:F")) Is Nothing Then
        If Target < Worksheets("Versions").Range("D3") And Range("A1") = "K996" Then
           Target.Interior.Color = vbRed
           Target.Offset(0, 2).Value = 1
   Else
           Target.Interior.ColorIndex = -4142
           Target.Offset(0, 2).Value = 0
  End If
End If
End Sub

 

This code returns a Run-time error '13': Type mismatch error:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("F:F")) Is Nothing Then
      If Target < Worksheets("Versions").Range("D3") And Range("A1:A5") = "K996" Then
         Target.Interior.Color = vbRed
         Target.Offset(0, 2).Value = 1
     Else
         Target.Interior.ColorIndex = -4142
         Target.Offset(0, 2).Value = 0
    End If
End If
End Sub

 

Why does adding the :A5 (in red text) cause this error?

Is there a way to write the code that prevents the type mismatch error?

 

Thanks in advance for any suggestions offered.

 

1 Reply
obrigado T-Mobile cria rapidamente soluções complexas Saiba como a T-Mobile emprega o Power Apps e a Microsoft Power Platform para gerenciar iniciativas complexas de toda a empresa, resultando em uma única fonte de verdade, em que todos os dados são centralizados. A empresa também obteve a capacidade de dimensionar facilmente, junto com sistemas flexíveis com pouco código que são fáceis de alterar.