EXCEL VBA Worksheet_Change 事件错误
小弟是VBA新手,如下这段代码是判断在A列单元格内输入的数据中的一部分是否和B1单元格一致,然后在B列对应的单元格显示正确与否,代码现在可以用,但是有个大的漏洞,就是当复...
小弟是VBA新手,如下这段代码是判断在A列单元格内输入的数据中的一部分是否和B1单元格一致,然后在B列对应的单元格显示正确与否,代码现在可以用,但是有个大的漏洞,就是当复制下拉A列或删除A列2行以上的单元格时,就会报错类型不匹配,如何解决这个问题呢,请指点!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row >= 10 And Target.Value <> "" Then
If Mid(Target, 2, 16) = [b1] Then
Target.Offset(0, 1) = "OK"
Else
Target.Offset(0, 1) = "NG"
MsgBox "输入错误"
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
End If
Exit Sub
End If
End Sub 展开
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row >= 10 And Target.Value <> "" Then
If Mid(Target, 2, 16) = [b1] Then
Target.Offset(0, 1) = "OK"
Else
Target.Offset(0, 1) = "NG"
MsgBox "输入错误"
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
End If
Exit Sub
End If
End Sub 展开
2个回答
展开全部
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row >= 10 And Target.Value <> "" And Target.Count = 1 Then
If Mid(Target, 2, 16) = [b1] Then
Target.Offset(0, 1) = "OK"
Else
Target.Offset(0, 1) = "NG"
MsgBox "输入错误"
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
End If
Exit Sub
End If
End Sub
追问
我试了,不行,朋友
追答
Private Sub Worksheet_Change(ByVal Target As Range)
if Target.Count = 1 then
If Target.Column = 1 And Target.Row >= 10 And Target.Value <> "" Then
If Mid(Target, 2, 16) = [b1] Then
Target.Offset(0, 1) = "OK"
Else
Target.Offset(0, 1) = "NG"
MsgBox "输入错误"
Application.EnableEvents = False
Target.ClearContents
Application.EnableEvents = True
End If
Exit Sub
End If
end if
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询