2

我的代码有什么问题,每次我删除工作表上的某些内容时,它都会给我一个运行时错误'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 
4

1 回答 1

2

在处理事件时始终使用Error handlingApplication.EnableEventsWorksheet_Change

如果提供的代码将 Range("G13") 转换为大写,则这里是更简化的代码。

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
于 2013-05-09T11:50:55.807 回答