当单元格的值更改(“True”到“False”或“False”到“True”)由我完成时,我尝试使用“Private Sub Worksheet_Change(byval Target As Range)”实现的例程效果很好,通过键盘,但当更改是其他单元格的结果时则不会。当更改单元格值是其他单元格的结果时,有什么方法可以调用宏吗?
我删除了正在使用的代码,但它仅响应键盘对目标单元格状态所做的更改。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells1 As Range
Dim KeyCells2 As Range
Set KeyCells1 = Range("D8") 'Seleção de ativo
Set KeyCells2 = Range("F42") ' Estado do alarme
If Not Application.Intersect(KeyCells1, Range(Target.Address)) _
Is Nothing Then
Macro1
End If
If Not Application.Intersect(KeyCells2, Range(Target.Address)) _
Is Nothing Then
Macro2
End If
End Sub
您需要在worksheet_calculate上触发:
Private Sub Worksheet_Calculate()
' Static variable to keep track of the previous value
Static prevValue As Variant
' Get the current value of the target cell
Dim currentValue As Variant
currentValue = Me.Range("A2").Value
' Check if the value has changed
If currentValue <> prevValue Then
' Your code goes here
MsgBox "The value of cell A1 has changed to: " & currentValue
' Update the previous value
prevValue = currentValue
End If
End Sub