Excel 运行时错误13,代码-清除单元格内容
目前,我正在运行一个电子表格,它告诉我是否选择了一名员工进行旅行。当我清除一个单元格上的内容时,我很好,但如果我一次清除多个单元格,则会出现运行时错误13。有没有办法消除这个错误Excel 运行时错误13,代码-清除单元格内容,excel,runtime-error,vba,Excel,Runtime Error,Vba,目前,我正在运行一个电子表格,它告诉我是否选择了一名员工进行旅行。当我清除一个单元格上的内容时,我很好,但如果我一次清除多个单元格,则会出现运行时错误13。有没有办法消除这个错误 Sub Worksheet_Change(ByVal Target As Range) Dim res As Variant If Not Intersect(Target, Range("A5:A550")) Is Nothing Then res = Application.CountIfs(Range("
Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Not Intersect(Target, Range("A5:A550")) Is Nothing Then
res = Application.CountIfs(Range("A5:A550"), Target, _
Range("M5:M550"), "Associate")
If Not IsError(res) Then
If res > 0 Then MsgBox "You have choosen an associate for this trip!"
End If
End If
End Sub
未经测试:
Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant, c as Range, rng as Range, i as long
Set rng = Application.Intersect(Target, Me.Range("A5:A550"))
If Not rng Is Nothing Then
i=0
For each c in rng.cells
res = Application.CountIfs(Range("A5:A550"), c.Value, _
Range("M5:M550"), "Associate")
If Not IsError(res) Then
If res > 0 Then i = i + 1
End If
Next c
If i > 0 Then
MsgBox "You have chosen an associate for " & _
IIf(i=1,"this trip!", i & " of these trips!")
End If
End If
End Sub
另一个对我有效的选项是在用户清除多个单元格或自动填充多个单元格的情况下退出Sub
Dim rng As Range
Set rng = Target.Parent.Range("A5:A550")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
// Put your code here
如果在一次操作中清除多个单元格,您希望发生什么?该如何处理?谢谢Tim Williams的帮助。“清除”内容允许在没有错误的情况下清除单元格,这正是我想要的(正如您所完成的)。