Search code examples
vbaexcelruntime-error

run-time error '13': type mismatch VBA 2010 to ensure uppercase text in cell


What's wrong with my code, every time I delete something on the worksheet it gives me a run-time error '13': type mismatch,

Private Sub Worksheet_Change(ByVal Target As Range)

     If Target.Address <> "$G$13:$J$13" Then
         If Target = Range("G13") Then
              test = UCase(Target.Value)
              If test <> Target.Value Then EnsureUppercase Target
         End If
     End If 
End Sub 

Solution

  • Always use Error handling and Application.EnableEvents when working with Worksheet_Change event

    If the code provided converts the Range("G13") to Upper Case here is more simplified code.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        On Error Resume Next
    
        Application.EnableEvents = False
    
        Dim rng As Range
        Set rng = Range("G13")
    
        If Not Intersect(Target, rng) Is Nothing Then
              Target.Value = UCase(Target.Value)
        End If
    
        Application.EnableEvents = True
    
    End Sub