“运行时错误'13'只有当”填补“使用?

下面的代码被devise为只允许在单元格范围内input1,2或3。 如果input了其他内容,则popup错误消息,并且条目被撤消。 代码完美工作,除非用户填写他们的回应。 此时,出现“运行时错误”13。 我希望用户能够填写他们的条目,是否有解决这个错误的方法?

Private Sub Worksheet_Change(ByVal Target As Range) Application.ScreenUpdating = False Application.Calculation = xlManual If Not Intersect(Target, [T7:AE61]) Is Nothing Then If (Target.Value Like "1") Then ElseIf (Target.Value Like "2") Then ElseIf (Target.Value Like "3") Then ElseIf (Not Target.Value Like "") Then MsgBox "Please enter a rating of 1, 2 or 3." Application.EnableEvents = False Application.Undo Application.EnableEvents = True End If End If Application.Calculation = xlAutomatic Application.ScreenUpdating = True End Sub 

提前感谢任何可能的帮助!

当它们执行填充时, Target是具有多个单元格的范围。 所以, Target.Value Like "1"失败,因为你正试图比较一个变体数组与string。 你需要做的是一次处理一个目标范围内的单个单元格。

 Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Range 'Added line Application.ScreenUpdating = False Application.Calculation = xlManual If Not Intersect(Target, [A1:AE61]) Is Nothing Then For Each cell In Intersect(Target, [A1:AE61]) 'Added line 'Within this loop, I have replaced Target with cell If (cell.Value Like "1") Then ElseIf (cell.Value Like "2") Then ElseIf (cell.Value Like "3") Then ElseIf (Not cell.Value Like "") Then MsgBox "Please enter a rating of 1, 2 or 3." Application.EnableEvents = False Application.Undo Application.EnableEvents = True End If Next cell 'Added line End If Application.Calculation = xlAutomatic Application.ScreenUpdating = True End Sub