我在尝试让这段代码工作时遇到了一些问题:
Private Sub Worksheet_Change(ByVal Target As Range)
Static ZeroFlag As Boolean
Dim KeyCells As Range
Set KeyCells = Range("B29")
On Error Resume Next
Set KeyCells = Application.Union(KeyCells, KeyCells.Precedents)
On Error GoTo 0
If Not Application.Intersect(Target, KeyCells) Is Nothing Then
If (Range("B29").Value <= 0) Xor ZeroFlag Then
MsgBox IIf(ZeroFlag, "Not zero", "zero")
ZeroFlag = Not (ZeroFlag)
End If
End If
End Sub
问题是,只有当我更改工作表中"B29“所在位置的值时,才会显示警报,但当我更改其他工作表中的值时,也需要显示警报。
例如:"B29“单元格位于工作表A上,是A1-A2的结果,A2从B工作表的单元格A1中获取其值,但只有当我从A工作表中更改单元格A2而不是从B工作表中更改A1时,才会显示警报。
我能做些什么来让它工作呢?
当我第一次开始处理这段代码时,我看起来是这样的,当我改变diferents表中的值时,它就可以工作了
Private Sub Worksheet_Calculate()
If Range("B29").Value <= "0" Then
MsgBox "Zero"
Else
MsgBox "Not Zero"
End If End Sub
但是这段代码没有我需要的规则。
在单元格大于0之前,警报不会显示,当值小于0时,警报显示"Zero“,只要值小于0,警报就会停止显示;值大于0时,警报就会显示”not Zero“,只要值保持在0以上,警报就会停止显示。
我能做些什么来让它工作呢?
谢谢
发布于 2016-03-17 11:30:32
而不是用Worksheet_Change编写代码,您必须用Workbook_SheetChange编写代码:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'....write your code here
End Sub
发布于 2016-03-17 14:40:59
我修改了我使用的第一个代码,并包含了一些其他代码,结果如下所示
Private Sub Worksheet_Calculate() Static ZeroFlag As Boolean
If Range("B29").Value <= "0" Xor ZeroFlag Then
MsgBox IIf(ZeroFlag, "Not Zero", "Zero")
ZeroFlag = Not (ZeroFlag)
End If
End Sub
感谢大家的帮助
https://stackoverflow.com/questions/36057863
复制相似问题