运行时错误“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
run-time error '13': type mismatch VBA 2010 to ensure uppercase text in cell
提问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 handling
and Application.EnableEvents
when working with Worksheet_Change
event
始终使用Error handling
和Application.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