1.Change事件
1.1 Change事件的触发时间
发生变化后触发本事件
- 与
SelectionChange的区别:
SelectionChange选中时触发本事件
1.2 Change事件的触发条件
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
Target.Value = "发生Change事件"
End Sub
1.3.事件的级联响应
注意: VBA对单元格的修改也会触发Change事件,避免级联响应
Application.Events属性
Application.Events属性
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 3 And Target.Column = 4 Then
If Not IsNumeric(Target) Or Target > 100 Or Target < 5 Then
MsgBox "武力值必须是0-100之间的数字"
Target.Select
Application.EnableEvents = False
Target.Value = "待输入"
Application.EnableEvents = True
End If
End If
End Sub