运行时错误“13”:键入不匹配的 VBA 2010 以确保单元格中的文本为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16456142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:02:20  来源:igfitidea点击:

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

vbaexcel-vbaruntime-errorexcel

提问by Adrian Gornall

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

我的代码有什么问题,每次我删除工作表上的内容时,它都会给我一个运行时错误“13”:类型不匹配,

Private Sub Worksheet_Change(ByVal Target As Range)

Private Sub Worksheet_Change(ByVal Target As Range)

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

End Sub

结束子

采纳答案by Santosh

Always use Error handlingand Application.EnableEventswhen working with Worksheet_Changeevent

始终使用Error handlingApplication.EnableEvents处理Worksheet_Change事件时

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

如果提供的代码将 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