作者:-而我知道阿信很忙 | 来源:互联网 | 2023-05-24 13:35
我正在编写一个简单的工作表,我需要根据用户输入更改单元格中的一些数据; 这些更改是使用该Worksheet_Change
事件进行的.然而,当我改变另一个细胞时,事件再次被触发,所以它变得非常令人头痛(这是一种"鸡与鸡蛋"的情景).
例:
private sub Worksheet_Change(ByVal Target as Range)
with target ' Only cells in column C are unlocked and available for edition
select case .row
case 4
if .value = 1 then
ActiveSheet.cells(5,3).value = 0
else
ActiveSheet.cells(5,3).value = 1
end if
case 5
if .value = 1 then
ActiveSheet.cells(4,3).value = 0
else
ActiveSheet.cells(4,3) = 1
end
end select
end with
end sub
正如您所看到的,第4行中的更改会触发第5行中的更改,这可能会触发第4行中的另一个更改...并且它将成为"无限调用",最终会崩溃excel.
所以,问题是:有没有办法以编程方式更改单元格的值而不触发Worksheet_Change
事件?
1> Gary's Stude..:
更改单元格时禁用中断,然后在完成后重新启用它们:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A1")
If Intersect(Target, A) Is Nothing Then Exit Sub
Application.EnableEvents = False
A.Value = ""
A.Offset(0, 1).Value = "CLEARED"
Application.EnableEvents = True
End Sub