问题:

要防止在区域A1:B6中输入无效值,如果出现错误,将显示相应的消息框。

答案:

在此工作簿模块中插入以下代码。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim var As Variant

If IsEmpty(Target) Then Exit Sub

If Intersect(Target, Range("Calculating")) _

Is Nothing Then Exit Sub

var = Application.Sum(Range("Calculating"))

If IsError(var) Then

Beep

MsgBox "Please enter the correct value!"

Target.Value = ""

End If

End Sub